21,886
社区成员
发帖
与我相关
我的任务
分享
ajaxform.php:
<table>
<tr>
<td>
userName:
</td>
<td>
<input type="text" name="user" id="user" value="" onchange="postValue()"/>
</td>
</tr>
<tr>
<td>
age:
</td>
<td>
<input type="text" name="age" id="age" value="" />
</td>
</tr>
<tr>
<td>
workTime:
</td>
<td>
<input type="text" name="worktime" id="worktime" value="" />
</td>
</tr>
</table>
<script>
var xmlHttp;
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;
}
function postValue(){
xmlHttp=GetXmlHttpObject();
if(xmlHttp==null) {
alert ("Browser does not support HTTP Request");
return;
}
var user=document.getElementById("user");
var url="get.php?";
var postdate="value="+user.value;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(postdate);
}
function stateChanged() {
if (xmlHttp.readyState==4 || xmlHttp.readyState=="200"){
var str_text=xmlHttp.responseText;
if (str_text == "")
alert("没有这个名字的数据");
else {
str_text = str_text.split('|');
document.getElementById("age").value = str_text[0];
document.getElementById("worktime").value = str_text[1];
}
}
}
</script>
get.php:
<?php
$arr = array(
'zhangsan'=>array(
'age'=>33,
'worktime'=>10
),
'lisi'=>array(
'age'=>34,
'worktime'=>11
),
'wangwu'=>array(
'age'=>35,
'worktime'=>12
),
'zhangliu'=>array(
'age'=>36,
'worktime'=>13
)
);
if (isset($_POST['value']) && $_POST['value'] != "") {
foreach($arr as $k=>$v) {
if ($k == trim($_POST['value'])) {
echo $v['age'] . '|'. $v['worktime'];
}
}
}
?>