Fabrizio Cacciapuoti
Fabrizio Cacciapuoti

Reputation: 43

Ajax not calling php script

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!

Upvotes: 0

Views: 108

Answers (1)

GeniusGeek
GeniusGeek

Reputation: 335

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

Upvotes: 2

Related Questions