JQ AJAX
用AJAX方法不刷新网页使用下拉列表连接数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="jquery-1.11.2.min.js"></script> </head> <body> <select id="sel"> </select> </body> <script type="text/javascript"> $(document).ready(function(e) { $.ajax({ url:"ajaxchuli.php", //处理页面 data:{}, //要提交的值 type:"POST", //提交方式 dataType:"TEXT", //返回类型 success: function(ss){ //回调函数 var hang=ss.split("|"); var str=""; for(var i=0;i<hang.length;i++) { var lie=hang[i].split("^"); str=str+"<option value='"+lie[0]+"'>"+lie[1]+"</option>" } $("#sel").html(str); } }); }); </script> </html>
<?php include("dbda.class.php"); $db=new dbda; $sql="select * from nation"; $attr=$db->Query($sql); $str=""; foreach($attr as $v) { $str=$str.implode("^",$v); $str=$str."|"; } $str=substr($str,0,strlen($str)-1); echo $str;
用AJAX方法实现判断账号是否已注册
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="jquery-1.11.2.min.js"></script> </head> <body> <div>用户名:<input type="text" id="uid" /> <span id="xx"></span></div> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#uid").blur(function(){ var uid=$(this).val(); //调AJAX $.ajax({ url:"uidchuli.php", data:{u:uid}, type:"POST", dataType:"TEXT", success: function(date){ if(date.trim()=="ok") { var str="可以使用"; $("#xx").html(str); $("#xx").css("color","#0F0"); } else { var str="已存在"; $("#xx").html(str); $("#xx").css("color","#F00"); } } }); }) }); </script> </html>
<?php $uid=$_POST["u"]; include("dbda.class.php"); $db=new dbda; $sql="select count(*) from users where uid='{$uid}'"; $attr=$db->Query($sql); if($attr[0][0]>0) { echo"no"; } else { echo"ok"; }