var xmlhttp function showuser(str) //这就是上面调用的函数体
{
xmlhttp=getxmlhttpobject() //首先判断浏览器是否支持http request
if (xmlhttp==null)
{
alert (browser does not support http request)
return
}
var url=getuser.php //这里是你的php函数名,就是这个函数里写的sql语句拉.
url=url+?q=+str //这是后面跟的参数
url=url+&sid=+math.random() //他在在这里创建了一个随机数
xmlhttp.onreadystatechange=statechanged //调用statechanged判断状态(这里的有些东西大家要查一下)
xmlhttp.open(get,url,true) //open传递进来的url true就是表示异步传输,就是页面不刷新
xmlhttp.send(null) //发送
}
function statechanged()
{
if (xmlhttp.readystate==4 || xmlhttp.readystate==complete) //判断readystate的状态
{
document.getelementbyid(txthint).innerhtml=xmlhttp.responsetext
//红色部分感觉熟悉吗?就是上面html页面里的id号.这里将返回的结果innerhtml的方法写进txthint
}
} //下面这个函数是针对不用浏览器对xmlhttprequest的不同支持而写的不同创建方法
function getxmlhttpobject()
{
var xmlhttp=null;
try
{
// firefox, opera 8.0+, safari
xmlhttp=new xmlhttprequest();
}
catch (e)
{
//internet explorer
try
{
xmlhttp=new activexobject(msxml2.xmlhttp);
}
catch (e)
{
xmlhttp=new activexobject(microsoft.xmlhttp);
}
}
return xmlhttp;
}
$q=$_get[q];
$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('could not connect: ' . mysql_error());
}
mysql_select_db(ajax_demo, $con);
$sql=select * from user where id = '.$q.';
$result = mysql_query($sql);
//下面的echo语句就是要返回给前台页面的html代码.最后就是下面这些html代码显示在id为
txthint的div里面
echo ;
while($row = mysql_fetch_array($result))
{
echo [tr];
echo [td] . $row['firstname'] . [/td];
echo [td] . $row['lastname'] . [/td];
echo [td] . $row['age'] . [/td];
echo [td] . $row['hometown'] . [/td];
echo [td] . $row['job'] . [/td];
echo [/tr];
}
echo [table][tr][td]firstname[/td][td]lastname[/td][td]age[/td][td]hometown[/td][td]job[/td][/tr][/table];
mysql_close($con);
?>