Diamond
Diamond

Reputation: 608

Jquery form plugin

I am using the Jquery form plugin to submit a login form data. This works well but I have problem redirecting a successful logged in user to the client panel from the login page in my PHP script, that is whenever the user is logged in successfully, I redirect using header("location:panel.php") in my php script, this page is sent back to the login page as a response and it will be embedded in the login page instead of showing as a whole page.

jquery code;

$(document).ready(function(){
    $("#loginform").ajaxForm({
        target: '#responselogin'
    }).submit();
});

php code;

<?php
$username=$_POST['username'];
$password=$_POST['password'];
if($username=="tarhe" && $password=="oweh") {
    header("location:panel.php");
} else {
    echo"login failed";
}
?>

Please I need help, thanks in advance

Upvotes: 2

Views: 155

Answers (3)

DevZer0
DevZer0

Reputation: 13535

your using ajax, so your 301 location header will have no effect to the overall browser window. you will have to return a structured data to your javascript and have javascript redirect instead.

$(document).ready(function(){
$("#loginform").ajaxForm({
success: function (data) {
   if (data == "login failed") {
       $("#responselogin").html("Login Failed");
   } else {
       window.location.href = "panel.php";
   }
}
).submit();
});

its actually better if you use json response for this. Since it's formatted better for browser consumption.

for handling this using json you can do the following. first change your php code to return a json response

$success = 0;
if($username=="tarhe" && $password=="oweh") {
   $success = 1;
}

echo json_encode(array('success' => $success));

Then in your javascript code

 $(document).ready(function(){
    $("#loginform").ajaxForm({
    type: 'json',
    success: function (data) {
       if (data.success == 0) {
           $("#responselogin").html("Login Failed");
       } else {
           window.location.href = "panel.php";
       }
    }
    ).submit();
    });

Upvotes: 3

SergkeiM
SergkeiM

Reputation: 4168

You are using ajax so make a response data and on success redirect the user.

 window.location.href = "somewhere.html";

Upvotes: 0

23tux
23tux

Reputation: 14736

I don't think you can use the location flag inside the http header with ajax. Think about it: You submit a form with ajax, so you're getting the response back in Javascript, and not into the browser.

Bind a success handler to the .submit() form, and set window.location.href=http://your-url.com

Upvotes: 0

Related Questions