2013-02-18 63 views
1

我试图去学习如何接收用ajax从数据库中的数据,和我有一些问题...为什么readyState不等于4?

的问题是,当我提交查询我没有看到任何东西... 我知道问题是因为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; 
?> 
+0

它显示标题吗?因为'$ display_string。=“​​$ row [ae_name]”;'也存在问题。它应该是:'“​​”。$ row [“ae_name”]。“”;'。 – 2013-02-18 03:09:24

回答

0

它看起来像两件事之一:

  • localhost正被用作域;它应该是127.0.0.1
  • ajax-example.php是在本地主机上运行,​​但相关的HTML文件不是
1

操作的顺序必须是:

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

你有错误的顺序项目2和3,所以事件处理程序正在被清除,永远不会被调用。

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

0

var queryString =“?age =”+ age +“& wpm =”+ wpm +“& sex =”+ sex;

你应该使用双和(& &) 尝试

VAR的queryString = “?年龄=” +年龄+ “& & WPM =” + WPM + “& &性别=” +性别;

相关问题