qadenza
qadenza

Reputation: 9293

conditional redirection doesn't work

$("#btnok").click(function(){
var pass = $('#inputpass').val();
$.ajax({
    url: 'passpro.php',
    type: 'post',
    data: {'pass': pass},
    success: function(data) {
        $('#info').text(data);
    }
});
});

The above works fine. Now I want to redirect to index.php if password exists, and echo a message if it doesn't.

passpro.php

include ("params.php");
include ("$config");

$pass = $_POST['pass'];

try {
$stmt = $db->prepare('SELECT id FROM passwords WHERE pass = :pass');
$stmt->execute(array(
':pass' => $pass
));

if ($stmt->rowCount() > 0) {
    header("Location: index.php"); // this line doesn't work
    exit();
}
else{
    echo ('UNKNOWN PASSWORD');
}
}
catch(PDOException $e) {
    echo $e->getMessage();
}

If rowCount == 0 this works fine, i.e. I got the message UNKNOWN PASSWORD, but if rowCount > 0 instead of redirection what I got is - html code of index.php written inside #info div !

Upvotes: 2

Views: 47

Answers (1)

Kenziiee Flavius
Kenziiee Flavius

Reputation: 1947

Passpro.php

if ($stmt->rowCount() > 0) {
    return 'true'
}
else{
    echo ('UNKNOWN PASSWORD');
}

Main Javascript

$.ajax({
    url: 'passpro.php',
    type: 'post',
    data: {'pass': pass},
    success: function(data) {
        if(data == 'true')
        {
            window.location.href = "http://stackoverflow.com"; //or whatever
        }
    }
});

I've not put an explanation because those who commented on your question have already explained

Upvotes: 1

Related Questions