views:

106

answers:

3

I am working on a simple AJAX page. when the page loads, it should take the result from the PHP page and display it in the text box. If the result is "1" (which it should be), then it should pop up an alert saying "Ready."

Main page's code (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>

The PHP page being called out to (t1_checkMatch.php) (all db info replaced with *):

<?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";
}
?>

When I go to the t1_wait.php?id=16 (the main page passing id=16 via GET), it should send a request to t1_checkMatch.php?id=16, which returns (yes, I checked) 1. This should trigger an alert saying "Ready" and cause 1 to appear in the text box, but neither of these things happen. The text box is blank.

What's wrong? Thanks!

+2  A: 

I believe the problem you are running into is due to a typo

xmlhttp.responceText

Really should be

xmlhttp.responseText

-- Update

It also appears that you are missing a {:

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

Should be

if(xmlhttp.responseText=="1"){
   alert("Ready!");
}
Jordan S. Jones
You also correct, but it didn't change anything.
Computerish
+1  A: 

You have a spelling mistake:

if(xmlhttp.responceText=="1")

should be:

if(xmlhttp.responseText=="1")

(you spelled response incorrectly)

Matt
A: 

Ok. I figured it out, but I don't know what I did. I did have a typo, but that isn't the problem. The PHP code is the same, here is the main page code:

<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>
Computerish
Is this a question, or is your problem resolved?
Matt
its resolved, i just wanted to put that there in case someone else has the same problem
Computerish