frosty
frosty

Reputation: 2669

Using AJAX to check variables in database, if true, header to a certain page, PHP, JAVASCRIPT

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:

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() { timeoutAjax(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";

?>

Upvotes: 0

Views: 90

Answers (1)

Duncan Thacker
Duncan Thacker

Reputation: 5188

From your question I'm assuming you want to redirect to the page that's returned from your AJAX call. You can't do this from PHP alone. Javascript:

$.ajax({
    method: "POST",
    url: someUrl
}).fail( function( error ) {
    alert( error );
}).done( function( response ) {
    window.location = response;
});

PHP:

<?php
     echo "test3.php";
?>

Upvotes: 1

Related Questions