Ajax返回随机值?

时间:2009-06-06 01:45:06

标签: php javascript ajax

我正在开发一个简单的AJAX页面。当页面加载时,它应该从PHP页面获取结果并在文本框中显示它。如果结果是“1”(它应该是),那么它应该弹出一个提示“准备就绪”。

主页的代码(t1_wait.php):

<html><head><title>Waiting...</title></head><body>

<script type="text/javascript">
function update(id)
{
   var xmlhttp;
   if (window.XMLHttpRequest){
         // code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
   }else if (window.ActiveXObject){
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }else{
      alert("Your browser does not support XMLHTTP!");
   }

   xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readyState==4){
         if(xmlhttp.responseText=="1")
            alert("Ready!");
         }
         document.myForm.status.value=xmlhttp.responseText;
      }
   }

   var requesturl = "t1_checkMatch.php?id="+id;
   xmlhttp.open("GET",requesturl,true);
   xmlhttp.send(null);

   // delay for 1 sec
   var date = new Date();
   var curDate = null;
   do { curDate = new Date(); }
   while(curDate-date < 1000);

}

<?php
   echo "update(".$_GET['id'].");";
?>

</script>


<form name="myForm">
Status: <input type="text" name="status" />
</form>

</body></html>

调用PHP页面(t1_checkMatch.php)(用*****替换所有数据库信息):

<?php
$db_user = "*****";
$db_pass = "*****";
$db_name = "*****";
mysql_connect(localhost,$db_user,$db_pass);
@mysql_select_db($db_name) or die("Unable to select database");

$match_id = $_GET['id'];

$match_info = mysql_query("SELECT * FROM ***** WHERE id=".$match_id);
if(mysql_result($match_info,0,"usr2")==-1){
   echo "1";
}else{
   echo "0";
}
?>

当我转到t1_wait.php?id = 16(主页通过GET传递id = 16)时,它应该向t1_checkMatch.php发送请求?id = 16,返回(是的,我检查过)1这应该触发一个提示“准备就绪”并导致1出现在文本框中,但这些都不会发生。文本框为空白。

怎么了?谢谢!

3 个答案:

答案 0 :(得分:2)

我认为您遇到的问题是由于拼写错误

  

xmlhttp.responceText

真的应该

xmlhttp.responseText

- 更新

您似乎错过了{

if(xmlhttp.responseText=="1")
   alert("Ready!");
}

应该是

if(xmlhttp.responseText=="1"){
   alert("Ready!");
}

答案 1 :(得分:1)

你有拼写错误:

if(xmlhttp.responceText=="1")

应该是:

if(xmlhttp.responseText=="1")

(你拼写错误的答案)

答案 2 :(得分:0)

确定。我想通了,但我不知道我做了什么。我确实有一个错字,但这不是问题。 PHP代码是相同的,这是主页面代码:

<html>
<body>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function update(id){
    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){
            if(ajaxRequest.responseText.indexOf("1")!=-1){
               document.myForm.status.value = "Ready!";
               window.location = "t1_game.php?id="+id;
            }else{
               document.myForm.status.value = "Waiting..."
               update(id);
            }
        }
    }
    ajaxRequest.open("GET", "t1_checkMatch.php?id="+id, true);
    ajaxRequest.send(null); 
}

<?php
echo "update(".$_GET["id"].");"
?>

//-->
</script>



<form name='myForm'>
Status: <input type='text' name='status' />
</form>
</body>
</html>