标签:
<!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> <h1>登录</h1> <div>用户名:<input type="text" id="uid" /></div> <div>密码:<input type="text" id="pwd" /></div> <div><input type="button" value="登录" id="btn" /></div> <div><input type="text" id="user" /><div id="ts"></div></div> <!--插入一个吞下错误数据的div--> <div><input type="text" id="erro" /></div> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#btn").click(function(){ var uid = $("#uid").val(); var pwd = $("#pwd").val(); $.ajax({ url:"chuli.php", //处理页面的路径 data:{u:uid,p:pwd,type:0}, //传递的数据 type:"POST", //数据的提交传递方式 datatype:"TEXT", //返回值的类型,TEXT,JSON,XML三种类型可选 success:function(data){ //回调函数 //检查是否会出错显示函数 $("#erro").html(date); if(data=="OK") { window.location = "Main.php"; } else { alert(data); } } }); }) //判断用户名是否可用 $("#user").blur(function(){ var uid = $(this).val(); $.ajax({ url:"chuli.php", data:{u:uid,type:1}, type:"POST", dataType:"TEXT", success: function(d){ if(d=="OK") { $("#ts").html("用户名可用"); } else { $("#ts").html("<span style=‘color:red‘>该用户名已经存在!</span>"); } } }); }) }); </script> </html>
<!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> <select id="sel"> </select> </div><br /> <input type="button" id="btn" value="出现" /> </body> <script type="text/javascript"> $(document).ready(function(e) { $("#btn").click(function(){ $.ajax({ url:"chuli.php", data:{type:2}, type:"POST", dataType:"TEXT", success: function(data){ var hang = data.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 $type = $_POST["type"]; include("DBDA.php"); $db = new DBDA(); switch($type) { case 0: $uid = $_POST["u"]; $pwd = $_POST["p"]; $sql = "select count(*) from login where username=‘{$uid}‘ and password=‘{$pwd}‘"; $attr = $db->Query($sql); if($attr[0][0]==0) { echo "用户名密码不正确!"; } else { echo "OK"; } break; case 1: $uid = $_POST["u"]; $sql = "select count(*) from login where username=‘{$uid}‘"; $attr = $db->Query($sql); if($attr[0][0] == 0) { echo "OK"; } else { echo "NO"; } break; case 2: $sql="select * from Nation"; $attr = $db->Query($sql); //n001^汉族|n002^回族|n003^苗族 $str =""; for($i=0;$i<count($attr);$i++) { for($j=0;$j<count($attr[$i]);$j++) { $str = $str.$attr[$i][$j]; $str = $str."^"; } $str = substr($str,0,strlen($str)-1); $str = $str."|"; } $str = substr($str,0,strlen($str)-1); echo $str; break; }
封装类
<?php class Ajax{ //ajax调用的方法 //sql是要执行的语句 //$type是SQL语句的类型,0代表增删改,1代表查询 //$db代表要操作的数据 public function Ajax($sql,$type=1,$db="testa") { //造连接对象 $conn = new mysqli($this->host,$this->uid,$this->pwd,$db); //判断连接是否成功 !mysqli_connect_error() or die("连接失败"); //执行SQL语句 $result = $conn->query($sql); $attr = $result->fetch_all(); //将数组拼成字符串 $str = ""; if($type==1){ for($i=0;$i<count($attr);$i++) { for($j=0;$j<count($attr[$i]);$j++) { $str = $str.$attr[$i][$j]; $str = $str."^"; } $str = substr($str,0,strlen($str)-1); $str = $str."|"; } $str = substr($str,0,strlen($str)-1); return $str."<br>"; } else { return $result; } } }
标签:
原文地址:http://www.cnblogs.com/Yue0327/p/5361661.html