我正在尝试在表单中输入姓名和电话,并根据输入值从 mysql 获取数据。当我通过单击功能运行查询时,浏览器会显示我的 php 和查询,但它不会显示来自数据库的值,而是显示“对象 HTMLInputElement”。
我的脚本中一定遗漏了一些东西,但不知道它是什么。当我提交这个 ajax/mysql 时,谁能告诉我为什么没有显示该值。请参阅下面的代码并寻求您的帮助...
HTML 和脚本
<script type="text/javascript" src="jquery-1.8.3.min.js"></script>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest;
try{
ajaxRequest = new XMLHttpRequest();
} catch (e){
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
alert("Your browser broke!");
return false;
}
}
}
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var age = document.getElementById('lname').value;
var queryString = "?lname=" + lname + "&phone=" + phone ;
ajaxRequest.open("GET", "find.php" + queryString, true);
ajaxRequest.send(null);
}
</script>
<form name='myForm'>
Last Name: <input type='text' id='lname' />
Phone: <input type='text' id='phone' />
<input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>
<div id='ajaxDiv'>Your result will display here</div>
</body>
</html>
PHP
$inputedname = $_GET['lname'];
$inputedphone = $_GET['phone'];
$inputedname = mysql_real_escape_string($inputedname);
$inputedphone = mysql_real_escape_string($inputedphone);
$query = "SELECT FirstName, Phone FROM ClientInfo WHERE LastName = '$inputedname' AND Phone = '$inputedphone'";
$qry_result = mysql_query($query) or die(mysql_error());
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Phone</th>";
$display_string .= "</tr>";
while($row = mysql_fetch_array($qry_result)){
$display_string .= "<tr>";
$display_string .= "<td>$row[FirstName]</td>";
$display_string .= "<td>$row[Phone]</td>";
$display_string .= "</tr>";
}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
在浏览器中