PHP+原生态ajax实现的省市联动功能详解(2)

<?php header("content-type:text/html;charset=utf-8"); $uname = $_GET['uname']; $link = mysql_connect("127.0.0.1","root",""); mysql_query("set names utf8"); mysql_select_db("test"); $sql = "select count(*) from t_user where name = '{$uname}'"; $rs = mysql_query($sql); if($row = mysql_fetch_array($rs)){ if($row[0] == 1){ echo "1";//己存在 }else{ echo "0";//不存在可以使用 } } mysql_free_result($rs); mysql_close($link); ?>

(小例子二)

post传值方式demo6.php

<?php header("content-type:text/html;charset=utf-8"); ?> <html> <head> <title>事件</title> </head> <body> <form action="#" method="post"> 用户名<input type="text" value=""/> <span></span><br /> 密码<input type="password" value=""/> <br /> <input type="submit" value="注册"/> <br /> </form> <script type="text/javascript"> function $(id){ return document.getElementById(id); } var http = null; var uname = $("uname"); uname.onblur = function(){ //1:创建对象 xmlhttprequest 对象 if(window.XMLHttpRequest){ http = new XMLHttpRequest(); }else{ http = new ActiveXObject("Microsoft.XMLHTTP"); } //2:准备url地址与参数 var url = "demo6_do.php"; //3:定义处理返回结果方法 http.onreadystatechange = result; //4:发送请求 http.open('POST',url,true); http.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); http.send("uname="+$("uname").value); }; //5:接收服务器返回结果 function result(){ //6:判断状态 接收完成 //7:判断状态 数据正确 if(http.readyState == 4 && http.status == 200){ //8:接收返回结果 {text/xml} var rs = http.responseText; var ms = ""; if(rs == 1){ ms = "己存在"; }else{ ms = "可以使用"; } //9:显示结果 $("msg").innerHTML = ms; } } </script> </body> </html>

demo6_do.php

<?php header("content-type:text/html;charset=utf-8"); $uname = $_POST['uname']; $link = mysql_connect("127.0.0.1","root",""); mysql_query("set names utf8"); mysql_select_db("test"); $sql = "select count(*) from t_user where name = '{$uname}'"; $rs = mysql_query($sql); if($row = mysql_fetch_array($rs)){ if($row[0] == 1){ echo "1";//己存在 }else{ echo "0";//不存在可以使用 } } mysql_free_result($rs); mysql_close($link); ?>

(小例子三)----xml

demo7.php

<?php header("content-type:text/html;charset=utf-8"); ?> <html> <head> <title>事件</title> </head> <body> <form action="#"> 省 <select> <option value='0'>--请选择--</option> <option value='1'>--河北--</option> <option value='2'>--河南--</option> <option value='3'>--广东--</option> </select> 市 <select> <option value='0'>--请选择--</option> </select> </form> <script type="text/javascript"> function $(id){ return document.getElementById(id); } var http = null; var sel = $("sel"); sel.onchange = function(){ //1:创建对象 xmlhttprequest 对象 if(window.XMLHttpRequest){ http = new XMLHttpRequest(); }else{ http = new ActiveXObject("Microsoft.XMLHTTP"); } //2:准备url地址与参数 var url = "demo7_do.php?shen="+$("sel").value; //3:定义处理返回结果方法 http.onreadystatechange = result; //4:发送请求 http.open('GET',url,true); http.send(null); }; //5:接收服务器返回结果 function result(){ //6:判断状态 接收完成 //7:判断状态 数据正确 if(http.readyState == 4 && http.status == 200){ //8:接收返回结果 {text/xml} var rs = http.responseXML; var citys = rs.getElementsByTagName("city"); for(var i = 0;i < citys.length;i++){ var o = document.createElement("option"); o.value = ""+(i+1); o.text = citys[i].firstChild.data; $("city").add(o,null); } //9:显示结果 } } </script> </body> </html>

demo7_do.php

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/46e92a7310d8a2d6c3f6a8c787426351.html