1

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.

Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45
user3438120
  • 71
  • 1
  • 6
  • 3
    AJAX calls occur "in the background" and any redirects that occur as part of the ajax call cannot affect the main page where the code that did the ajax call is executing from. If you want an ajax response to redirect the page, you'll have to have your ajax success handler do a `window.location = ....` type redirect yourself. – Marc B Apr 09 '14 at 15:01
  • **Clear** duplicate of [How to manage a redirect request after a jQuery Ajax call](http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call) – MonkeyZeus Apr 09 '14 at 15:02
  • Thanks, but Can you tell me at least if the session will get established? – user3438120 Apr 09 '14 at 16:08

1 Answers1

1

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

Amrit Shrestha
  • 1,620
  • 20
  • 25
  • Thanks, but Can you tell me at least if the session will get established? – – user3438120 Apr 09 '14 at 16:08
  • 1
    yes, the session will get set. refer to link below: [link]http://stackoverflow.com/questions/676846/do-ajax-requests-retain-php-session-info in your code: you can access anything you echo on php on "response" variable on your javascript.. success: function(response) { jQuery("#phplogin").html(response).show('normal'); jQuery('#inputEmail1, #inputPassword1').val(""); } – Amrit Shrestha Apr 12 '14 at 12:30
  • Yes, thank u It solved my problem. I was looking for the exact same thing. – Prabhu Jun 07 '14 at 10:48