Jump to content

Redirect if ajaxRequest.responseText ==1


conan318

Recommended Posts

<script language="javascript" type="text/javascript">

function updateq(){
  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){
      var ajaxDisplay = document.getElementById('ajax');
     
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
       if ajaxRequest.responseText == '1'{
      	window.location = 'chat.php'
      }
    }
  }
var queryString = "?update=queue";
  ajaxRequest.open("GET", "ajax-chat.php" + queryString , true);
  ajaxRequest.send(null); 

  

}

//-->

</script>

Link to comment
https://forums.phpfreaks.com/topic/257671-redirect-if-ajaxrequestresponsetext-1/
Share on other sites

is my go at using ajax so bare with me as get my head around it.

 

ok im new to world of ajax.

 

iam trying to make a small one on one help chat. if 2 users are in the chat anyone after that gets put into a queue.

when the admin end the chat  i have function which removes the other user from the chat and from queue table in my mysql database.

 

that part works fine.

 

i have a second function running on a javascript timer which updates queue.

so then  i am trying to redirect the user that is next in line depending on the response.

 

if response text == 1 redirect that user into the chat room.

 

ajax


<script language="javascript" type="text/javascript">

function updateq(){
  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){
      var ajaxDisplay = document.getElementById('ajax');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
    
       if(ajaxDisplay.innerHTML == "1"){ 

window.location.replace="chat.php"; 
exit; 


    }
  }
var queryString = "?update=queue";
  ajaxRequest.open("GET", "ajax-chat.php" + queryString , true);
  ajaxRequest.send(null); 

  

}


//-->
</script>

 

ajax-chat.php


if ($update=="queue"){

$result = mysql_query("SELECT * FROM  q"); 
$num_rows = mysql_num_rows($result);

if ($num_rows >2){
echo "Welcome ".$_SESSION['myusername']." there are ".$num_rows." people ahead of you in the queue";

}else{
echo "1";
}
}

 

 

let me know if you need more info

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.