我是ajax新手。我有一个从数据库和 4 个文本框填充的下拉列表。当用户从下拉列表中选择标题时,文本框应自动填充。
这是我的 JavaScript 代码。
function getXMLHttp()
{
var xmlHttp
try
{
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
//Internet Explorer
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!")
return false;
}
}
}
return xmlHttp;
}
function MakeRequest(username)
{
var xmlHttp = getXMLHttp();
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
{
HandleResponse(xmlHttp.responseText);
}
}
xmlHttp.open("GET","ajax.php?username="+username,true);
xmlHttp.send(null);
}
function HandleResponse(response)
{
document.getElementById("f").value = response;
}
这是我的 php 代码
require_once'/classes/USRclass.php';
if(isset($_GET['username']))
{
$username=$_GET['username'];
$usr1=new USER;
$where="username='$username'";
$a=$usr1->show($where);
echo $a['name'];
echo $a['family'];
echo $a['email'];
echo $a['privilege'];
}
这是我的表格
<form method="get">
username:<select name="username" onChange="MakeRequest(this.value)">
<option></option>
<?php
require_once'/classes/USRclass.php';
$usr1=new User;
$a=$usr1->show_all_field('username');
if(count($a)==1)
{
foreach($a as $key)
echo"<option>$key</option>";
}
else if(count($a>1))
{
foreach($a as $key)
foreach($key as $column=>$val)
echo"<option>$val</option>";
}
?>
</select><br>
name: <input id="n" type="text" name="u_name"><br>
family: <input type="text" name="u_family" id="f"><br>
email: <input type="text" name="u_email"><br>
privilege: <input type="text" name="u_privilege"><br>
<input type="button" name="u_edit" value="Edit">
<input type="submit" name="u_ok" value="OK">
</form>