我不会吧 JS 和 php 在这上面关联起来, 同学们可以新建两个文件分贝复制粘贴, 数据库的配置等我就不讲了, 自己动手查查学习印象深刻.
javascript 代码
- <!DOCTYPE html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Untitled Document</title>
- <script type="text/javascript">
- function validation()
- {
- var tel = document.getElementById("telephone").value;
- var pwd = document.getElementById("password").value;
- var postStr = "telephone="+tel+"&password="+pwd;
- ajax("7.php",postStr,function(result){
- document.getElementById("info").innerHTML="1"+result+"1";
- });
- }
- function ajax(url,postStr,onsuccess)
- {
- var xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
- xmlhttp.open("POST", url);
- // document.write("5");
- xmlhttp.onreadystatechange = function (){
- // document.write("1");
- if (xmlhttp.readyState == 4){
- // document.write("~");
- if (xmlhttp.status == 200){
- onsuccess(xmlhttp.responseText);
- }
- else
- {
- alert("AJAX 服务器返回错误!");
- }
- }
- }
- // document.write("2");
- xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
- // 不要以为 if (xmlhttp.readyState == 4) {在 send 之前执行!!!!
- // document.write("3");
- xmlhttp.send(postStr);
- // document.write("4"); // 这时才开始发送请求. 并不等于服务器端返回. 请求发出去了, 我不等! 去监听 onreadystatechange 吧!
- }
- </script>
- </head>
- <body>
- 用户名:<input id="telephone" name="telephone" type="text" />
- <BR />
- 密码:<input id="password" name="password" type="password" />
- <BR />
- <input type="button" name="button" value="提交" onclick="validation();" />
- <div id="info"></div>
- </body>
- </html>
下面是 PHP 代码
PHP 代码
- <?php
- $con = mysql_connect('localhost','root','');
- if(!$con)
- {
- die('error:'.mysql_error());
- }
- mysql_select_db("diuba",$con);
- $result = mysql_query("select * from diuba_users where telephone ='$_POST[telephone]'");
- while($row = mysql_fetch_array($result))
- {
- if($row['password'] == $_POST['password'])
- {
- echo 'success';
- }
- else
- {
- echo 'error';
- }
- }
- ?>
来源: http://www.qdfuns.com/article/31246/31e37dbd9f1ef47136d236c4dfd18194.html