user3438120
user3438120

Reputation: 71

PHP Redirect in Ajax call

I have an Ajax call, that posts the values of a login form to a php file and will update the div id= #phplogin above username and password with error message if the username and password are wrong.

With in the same PHP file, if the username and password are correct, then I am redirecting the page. But this redirection is happening within the div, the whole page is not getting redirected. Now I am seeing the redirected page inside the div above username and password. How can this be avoided and redirected completely?

Ajax call:

jQuery("#loginForm").validate({
    submitHandler: function() {

    var postvalues =  jQuery("#loginForm").serialize();

    jQuery.ajax
    ({
        type: "POST",
        url: "contact/process-loginform.php",
        data: postvalues,
        /* success: function(response)
        {
            jQuery("#phplogin").html(response).show('normal');
            jQuery('#inputEmail1, #inputPassword1').val("");
        }*/
        error: function(response)
        {
           jQuery("#phplogin").html(response).show('normal');
           jQuery('#inputEmail1, #inputPassword1').val("");
        }
    });

Here is the php part of code:

if(mysql_num_rows($result) == 0) {  // Combo not found
            $phplogin =  '<div class="alert alert-danger fade in">Invalid Username/Password. Please try again.</div>';
            die($phplogin);
        }
        else
        {
         session_regenerate_id();
         $_SESSION['sess_user_id'] = $result['id'];
         $_SESSION['sess_username'] = $result['username'];
         session_write_close();
         header('Location: ../index.php');
        }

Now, How can I display error when mysql_num_rows($result) == 0 and how can I redirect completely when mysql_num_rows($result) != 0.

Please help.

Upvotes: 1

Views: 5278

Answers (1)

Amrit Shrestha
Amrit Shrestha

Reputation: 1660

anything you print out in php through ajax will be printed only. the redirect code in php won't work here. you can send some kind of status message from php and check that on your ajax call. BTW, it will return on success

Upvotes: 1

Related Questions