weixin_33736048 2012-12-29 20:11 采纳率: 0%
浏览 53

ajax检索mysql数据

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

enter image description here

  • 写回答

1条回答 默认 最新

  • weixin_33708432 2012-12-29 20:18
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题