0

I'm using Ajax for the first time to call a php script. Unfortunately the script doesn't work. None of his instructions are executed. Here is the script where I use Ajax:

<?php
    if (isset($_GET['errore'])) {
        switch ($_GET['errore']) {
            case 'authfailed':
                echo 
                '<script>
                $("#status").html("Credenziali Errate");
                $("#status").css("color","red"); 
                setTimeout(function(){
                    $("#status").html("Inserisci i dati forniti dall\'amministratore di sistema"); 
                    $("#status").css("color","black"); 
                    $.ajax({ 
                        url: "../php/redirect.php",
                        data: {action: "redirect_area_personale"},
                        type: "POST",
                        success: function() {}
                    });
                }, 5000);
                </script>';
                break;                
        }
    }       
?> 

And here is the script called:

<?php
if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'redirect_area_personale': 
            header("Location: ../pages/area_personale.php");
            die();
            break;
    }
}
?>

Console doesn't display any error. It could be a silly mistake but that's my first try so any help is very appreciated!

9
  • First step (as often) is to have a look to dev tool, especially the Network tab. Do you see your request ? If so what is the status of the response ? Commented Apr 24, 2020 at 11:26
  • Check network tab, to see if the error is from ajax or php, if the network status is 200 then it is from php, if it is otherwise then is from the ajax or your first php script. Also try adding action: "action" to ajax script $.ajax({ action :" action", url: "../php/redirect.php", data: {action: "redirect_area_personale"}, type: "POST", success: function() {} }); }, 5000); To go to network tabs click th3 3 dots on browser->more tools->.... Commented Apr 24, 2020 at 13:00
  • @CedricCholley It doesn't show any error. I can even see that the script containg the ajax call and the script with the redirect are successfully called. They seem to be working, but no redirect to area_personale.php Commented Apr 25, 2020 at 18:58
  • 1
    okay, i understand what's going on now, ajax call to php expects a value not a redirect, so the error and thus not redirecting. Your redirect should be with ajax JavaScript on success function. So in area_personale.php echo out a value. Get this value in ajax and use window.location to redirect Commented Apr 25, 2020 at 20:42
  • 1
    @GeniusGeek Sorry for being late. You got it! You should write an answer so I can flag it as correct. Commented May 7, 2020 at 9:25

1 Answer 1

2

Ajax needs a response from PHP and not a redirect. Your redirect should be done in the ajax success function using window.open or window.location.href like this.

 success: function(){
window.open("../path/area_personale.php", "_self");
}

or using window.location.href

  success: function(){
window.location.href="../path/area_personale.php";
}
Sign up to request clarification or add additional context in comments.

Comments

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.