假设此示例HTML:
<input type="text" name="email" id="email" />
<input type="text" name="first_name" id="first_name" />
<input type="text" name="last_name" id="last_name" />
您可以使用以下javascript:
$("#email").bind("change", function(e){
$.getJSON("http://yourwebsite.com/lokup.php?email=" + $("#email").val(),
function(data){
$.each(data, function(i,item){
if (item.field == "first_name") {
$("#first_name").val(item.value);
} else if (item.field == "last_name") {
$("#last_name").val(item.value);
}
});
});
});
然后就是一个PHP脚本(在本例中是lookup.PHP),它在查询字符串中接收一封邮箱,并返回一个JSON格式的数组,其中包含要访问的值.这是实际访问数据库以查找值的部分:
<?php
//look up the record based on email and get the firstname and lastname
...
//build the JSON array for return
$json = array(array('field' => 'first_name',
'value' => $firstName),
array('field' => 'last_name',
'value' => $last_name));
echo json_encode($json );
?>
你会想做一些其他的事情,比如清理邮箱输入等等,但应该让你朝着正确的方向前进.