2

I'm trying to use AJAX to check variables in the database, and if it's true, then it should make it header to a certain page, except in this testing phrase, I'm not checking any variables. I'm just testing if it'll header off to that certain page if I call the function. I started at test1.php, but it should've called the ajax function, and immediately header off to test3.php, but it didn't. I'm not sure what I did wrong. Please take a look at my code:

ajax.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 

function nopassAjax(url,timeout) {

      $.ajax({
           type: "POST",
           url: url,
           error: function(xhr,status,error){alert(error);},
           success:function(data) {
             setTimeout(function() { nopassAjax(url,timeout); }, timeout);
           }

      });

}

</script>

test1.php

<?php

include('ajax.php');

echo "<script>";

echo "nopassAjax('test2.php',1000);";

echo "</script>";

?>

test2.php

<?php

//checks some stuff in the database
//if true, header off to test3.php

header("Location: test3.php");

?>

test3.php

<?php

echo "Hello";

?>
2
  • 1
    What's that function timeoutAjax()? Where is it defined? What does it do? Commented Jun 18, 2015 at 0:06
  • @MarcosDimitrio Sorry. It was suppose to be nopassAjax. Commented Jun 18, 2015 at 0:08

1 Answer 1

1

This is not going to work as you expect. What will happen is, when you hit test1.php, it will trigger nopassAjax(), which will make an AJAX request to test2.php. The test2.php script will call header("Location: test3.php"); but as a response to the AJAX request, so it's the nopassAjax() that will receive the contents of test3.php, not the calling page test1.php. Then you setTimeout to start it over, so it will be in a loop and never get to where you want.

I suggest you do it a bit different. Make an AJAX call from test1.php to test2.php, but from there you return a JSON object with the information from the database. When test1.php receives it, it will check the data and make the decision to redirect or not.

Could be something like this:

test1.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script type = "text/javascript"> 
$(document).ready(function() {
    setMonitoring("test2.php", 100);
});

function setMonitoring(url, timeout) {
    setTimeout(function(){
        checkData(url);
    }, timeout);
}

function checkData(url) {
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json", // type of data that you're expecting back from the server
        error: function(xhr,status,error){alert(xhr.status + " - " + error);},
        success:function(data) {
            if (data.redirect) {
                window.location.href = data.redirectTo;
            }
            setMonitoring(data.nextUrlToQuery, data.timeout);
        }
    });
}
</script>

test2.php

// retrieve the information you need...

$result = array(
    "redirect"=>false,
    "nextUrlToQuery"=>"test2.php",
    "redirectTo"=>"test3.php",
    "timeout"=>1000
);
echo json_encode($result);

test3.php

echo "Hello";

In this example, when you change redirect to true in test2.php, the page will redirect to test3.php.

Sign up to request clarification or add additional context in comments.

8 Comments

This is good, but is there any way I can pass the url,timeout, and the redirect url in through the parameters, somehow?
Also, is there any way to have the setTimeout in the checkData function so I can pass the timeout right through the parameters?
You can send the url and timeout through the JSON object, I have updated my answer. The checkData() function is already setting the timeout in the line setMonitoring(data.timeout);, I've just made an extra function because the same code would be called two times, there and on $(document).ready().
What about the url for test2.php?
This is a really good answer. I just have one more question, but I have bothered you enough, so I'll just ask it elsewhere. Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.