当前位置: 动力学知识库 > 问答 > 编程问答 >

html - ajax retrieve mysql data

问题描述:

I am trying to input name and phone in form and get data from mysql based on input values. When I run query by on click function the browser displays my php and query but instead of values from database it displays 'object HTMLInputElement'.

I must be missing something in my script but can't figure out what it is.

Can anybody tell me when I submit this ajax/mysql why the value is not being displayed. See code below and hanks for your help...

HTML and SCRIPT

<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;

In Browser

网友答案:

That's because you never defined the variables lname and phone in your var queryString = "?lname=" + lname + "&phone=" + phone ; line. Therefore, browsers generate variables from your input element IDs. When you use a DOM element in string concatenation, its toString() is called and it outputs [object HTMLInputElement]. That's a feature that IE gave us from the early days and other browsers copied to be IE compatible. It's a feature you should not use.

The following code will fix your problem.

var lname = document.getElementById('lname').value;
var phone = document.getElementById('phone').value;
var queryString = "?lname=" + lname + "&phone=" + phone ;
ajaxRequest.open("GET", "find.php" + queryString, true);

As an aside, to prevent SQL injection, you should use prepared statements instead of http://php.net/manual/en/function.mysql-real-escape-string.php which is deprecated

分享给朋友:
您可能感兴趣的文章:
随机阅读: