ajax.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<select id='text' onchange="return fun()">
<option value="光头强">光头强</option>
<option value="熊二">熊二</option>
<option value="熊大大">熊大大</option>
<option value="萝卜头">萝卜头</option>
</select>
<!-- 名字:<input type="text" id="name" name="name">
<button type="button" onclick="load()">点击</button>-->
<div id="myDiv"></div>
<script>
function fun() {
var name = document.getElementById("text").value;
// 1.创建XMLHttpRequest对象
var aja = new XMLHttpRequest();
// 2.请求行
aja.open("POST", "./ajax.php");
// 3.请求头
aja.setRequestHeader('Content-Type',' application/x-www-form-urlencoded');
// 4.设置数据
aja.send("name="+name);
// 5.监听服务器响应
aja.onreadystatechange = function(){
if (aja.readyState==4 && aja.status==200){
document.getElementById("myDiv").innerHTML=aja.responseText;
}
}
}
</script>
</body>
</html>
ajax.php
<?php
include 'config.php';
echo $name= $_POST["name"];
$sql="select * from stu where name='$name'";
$query=mysqli_query($conn,$sql);
echo "姓名:".$name.'<br/>';
while($rs=mysqli_fetch_assoc($query))
{
echo "性别:".$rs['sex']."<br/>";
echo "生日:".$rs['birthday']."<br/>";
echo "电话:".$rs['phone']."<br/>";
echo "简介:".$rs['jianjie']."<br/>";
}
?>
onfig.php(连接数据库的代码)
<?php
$conn=mysqli_connect('localhost','root','root','www') or die('数据库连接失败');
mysqli_set_charset($conn,'utf8');
?>
先看一下我的数据库: