1

我试图学习如何使用ajax从数据库接收数据,但我遇到了一些问题......

问题是当我提交查询时我没有看到任何东西...我知道问题是因为 readyState 不等于 4...但是为什么呢?

我在 index.html 文件中有这段代码:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }
    var age = document.getElementById('age').value;
    var wpm = document.getElementById('wpm').value;
    var sex = document.getElementById('sex').value;
    var queryString = "?age=" + age + "&wpm=" + wpm + "&sex=" + sex;
    ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>



<form name='myForm'>
   Max Age: <input type='text' id='age' /> <br />
   Max WPM: <input type='text' id='wpm' />
   <br />
   Sex: <select id='sex'>
          <option>m</option>
          <option>f</option>
        </select>

    <input type='button' onclick='ajaxFunction()' value='Query MySQL' />
</form>

以及 ajax-example.php 文件中的这段代码:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "*************";
$dbname = "*************";
    //Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
mysql_select_db($dbname) or die(mysql_error());
    // Retrieve data from Query String
$age = $_GET['age'];
$sex = $_GET['sex'];
$wpm = $_GET['wpm'];
    // Escape User Input to help prevent SQL Injection
$age = mysql_real_escape_string($age);
$sex = mysql_real_escape_string($sex);
$wpm = mysql_real_escape_string($wpm);
    //build query
$query = "SELECT * FROM ajax_example WHERE ae_sex = '$sex'";
if(is_numeric($age))
    $query .= " AND ae_age <= $age";
if(is_numeric($wpm))
    $query .= " AND ae_wpm <= $wpm";
    //Execute query
$qry_result = mysql_query($query) or die(mysql_error());

    //Build Result String
$display_string = "<table>";
$display_string .= "<tr>";
$display_string .= "<th>Name</th>";
$display_string .= "<th>Age</th>";
$display_string .= "<th>Sex</th>";
$display_string .= "<th>WPM</th>";
$display_string .= "</tr>";

    // Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){
    $display_string .= "<tr>";
    $display_string .= "<td>$row[ae_name]</td>";
    $display_string .= "<td>$row[ae_age]</td>";
    $display_string .= "<td>$row[ae_sex]</td>";
    $display_string .= "<td>$row[ae_wpm]</td>";
    $display_string .= "</tr>";

}
echo "Query: " . $query . "<br />";
$display_string .= "</table>";
echo $display_string;
?>
4

3 回答 3

1

操作顺序必须是:

  1. 创建 AJAX 对象
  2. 打开连接
  3. 设置 readystatechange 处理程序
  4. 发送请求。

您的项目 2 和 3 的顺序错误,因此事件处理程序正在被清除并且永远不会被调用。

编辑:还应该指出的是,new XMLHttpRequest自 2007 年以来,随着 IE7 的发布,它已经完全跨浏览器。您现在不需要使用那些 ActiveXObjects。

于 2013-11-27T01:25:41.693 回答
0

它看起来像两件事之一:

  • localhost被用作域;它应该是127.0.0.1
  • ajax-example.php正在本地主机上运行,​​但关联的 HTML 文件不是
于 2013-11-27T01:18:24.310 回答
0

var queryString = "?age=" + 年龄 + "&wpm=" + wpm + "&sex=" + 性别;

你应该使用 double and(&&) 试试看

var queryString = "?age=" + 年龄 + "&&wpm=" + wpm + "&&sex=" + 性别;

于 2016-03-09T16:59:23.397 回答