PHP语言 -- Ajax 查询数据

<body>

<select id="sel">

</select>

<div><br/>
<input type="button" id="btn" value="出现"/></div>

</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>

 

 

 

chuli.php

<?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][$i];
$str=$str."^";
}
$str=substr($str,0,strlen($str)-1);
$str=$str."|";
}
$str=substr($str,0,strlen($str)-1);

echo "$str";

break;
}

 

posted on 2016-04-25 16:37  一方通行o  阅读(400)  评论(0编辑  收藏  举报

导航