Ajax Success - actions at the time of Ajax’s response

Asked

Viewed 183 times

1

Let’s go: During AJAX communication with my PHP I need to test a condition and if the condition is satisfied I want the page to go back to index.html

    $('#tabela').empty();
    $retorno=0;
    $.ajax({
        data: {funcao: 'listaMeusEstabs', idcli: getUrlVars()["idcli"], idsess: getUrlVars()["idsess"]},
        type:'post',    
        dataType: 'json',
        url: $servidor,
        success: function(dados){
            $retorno=1;
            $('#tabela').append("<tr><td> ---TUDO OK---</td></tr>");                    
        },
        complete: function(dados){
            if ($retorno==0){
                $linha = "<tr><td>Sem registros</td></tr>";
                $('#tabela').append("<tr><td> -SEM REGISTROS-</td></tr>");
            }
            // *** AQUI PRECISO DE UMA FUNÇÃO QUE JOGUE A PAGINA ATUAL PARA INDEX.HTML
        },
    });

In PHP I have the following condition:

        if ($intruso==1){
            //manda o usuario para INDEX.HTML
        }else{
            $sql = " SELECT * FROM tbl_cartao"
            $qryLista = mysqli_query($con, $sql);   
            while($resultado = mysqli_fetch_assoc($qryLista)){
                $vetor[] = array_map('utf8_encode', $resultado); 
            }   
            echo json_encode($vetor);
        }

1 answer

1

You can use the window.location for redirecting in AJAX. See:

if(sua condicao aqui dentro){
    window.location = "index.html";
}

If you want to redirect in PHP, you can do it this way:

echo "<script>location.href='index.html';</script>"; 

Or use the header:

header('Location: index.html');

The observation is that you have to check exactly where your index.html is, to be redirected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.