我正在尝试创建一个具有自动填充功能的表单,其中用户输入员工ID,并且当他们离开员工ID字段时填充名称,职位,状态等字段,类似于http://www.crackajax.net/popform.php#中的表格。稍后将数据提交到另一个数据库以跟踪其他用户输入的信息。但是,当我汇编文件时,我似乎无法使用onchange功能。以下是表格:
表单本身:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<meta charset="utf-8" />
</head>
<body>
<form id="entry" name="entry" method="post">
<label for="employeeid" class="stdlabel">Employee ID:</label>
<input type="text" id="employeeID" name="employeeID" onchange="geteedata()">
<br>
<label for="employeename" class="stdlabel">Employee Name:</label>
<input id ="employeename" type="text" name="employeename">
<br>
<label for="employeestatus "class="stdlabel">Status:</label>
<input type="text" name="employeestatus" >
<br>
<label for="employeetitle"class="stdlabel">Job Title:</label>
<input type="text" name="employeetitle" >
<br>
<label for="loccode" class="stdlabel">Employee Loc Code:</label>
<input type="text" name="loccode" >
<input type="submit" name ="submit" value="Submit" style="margin-right:10px">
</form>
<?php
if(isset($_POST['submit'])){
$con = mysql_connect("localhost", "root", "xxx");
if(!$con) {
die("Cannot connect to database:" . mysql_error());
}
mysql_select_db("mydb", $con);
$sql = "INSERT INTO casetracker (emplid,empname,empjobtitle,status, loccode) VALUES ('$_POST[employeeid]','$_POST[employeename]','$_POST[employeetitle]','$_POST[employeestatus]','$_POST[loccode]')";
mysql_query($sql,$con) or die(mysql_error());
mysql_close($con);
?>
</body>
</html>
以下是javascript文件:
function geteedata() {
var url = "search.inc.php?param=";
var idValue = document.getElementById("employeeID").value;
var myRandom = parseInt(Math.random() * 99999999); // cache buster
http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
function handleHttpResponse() {
if (http.readyState == 4) {
results = http.responseText.split(",");
document.getElementById('employeename').value = results[0];
document.getElementById('employeestatus').value = results[1];
document.getElementById('employeetitle').value = results[2];
document.getElementById('loccode').value = results[3];
}
}
这是search.inc.php:
if(strlen($param)>0){
$result = mysql_query("SELECT * FROM employee_data WHERE Pernr ='$param'");
if(mysql_num_rows($result)==1) {
while($myrow = mysql_fetch_array($result)){
$employeename = $myrow["Last_First"];
$employeestatus = $myrow["Emp_Status"];
$employeetitle = $myrow["Position_Text"];
$loccode = $myrow["loccode"];
$textout .= $employeename.",".$employeestatus.",".$employeetitle.",".$loccode;
}
} else {
$textout=" , , ,".$param;
}
}
echo $textout;
有没有人对我忘记,遗失或做错的事情有所了解?
提前致谢!
答案 0 :(得分:0)
在发送请求之前,您似乎没有创建XMLHttpRequest对象的实例。
尝试以下方法:
function geteedata() {
var url = "search.inc.php?param=";
var idValue = document.getElementById("employeeID").value;
var myRandom = parseInt(Math.random() * 99999999); // cache buster
var http = new XMLHttpRequest();
http.open("GET", url + escape(idValue) + "&rand=" + myRandom, true);
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
有关详细信息,请查看MDN Using XMLHttpRequest。