0

I am using a login system for my website. What I am doing is that when the user clicks the submit button an ajax request is being executed to target php script which logs in the user and if user is logged in successfully then echo's back the message "login_successful" to the ajax request. By using if statement I check whether the message is "login_successful" else display the error.

If the message is "login_successful" the script redirect the user to the new page 'user.php' by using window.location = 'user.php'; //I have also tried this. window.location.href = 'user.php';

But it doesn't work It simply stays on the login page and nothing happens. but when i check the page source after logging in but no redirection takes place then i got a surprise that the source of the page is for user.php instead of login.php. Somehow window.location hasn't redirected the page but bring the source of user.php to login.php. It's all messed up and I couldn't solve the problem.

Here is the login function which performs the ajax request and then redirect the user-

function login()
{
    var e = _("email").value;// Grab the email input by the user.
    var p = _("password").value;// Grab the password input by the user.
    if(e === "" || p === "")// Check if they are empty.
    {
        _("status").innerHTML = "Fill out all the form data";//Display error message if fields are empty.
    }
    else
    {
        _("loginbtn").style.display = "none";// Hide the login button
        _("status").innerHTML = 'please wait ...';//Tell the user to wait while the script works.
        // Below id another function which is defined in other js file.
        var ajax = ajaxObj("POST", "login.php"); //start the ajax request.
        ajax.onreadystatechange = function()// Wait for the request to be complete
        {
            if(ajaxReturn(ajax) === true) // Ensures if the response is recieved
            {
                var response = ajax.responseText;// Put the response in a variable
                if(response === 'login_successful')// Check if user is logged in succesfully.
                {
                    window.location = 'user.php';//Redirect the user to the new page.(Not working)
                }
                else
                {
                    _('status').innerHTML = response;// Display the response
                    _("loginbtn").style.display = "block";// Display the login button
                }
            }
        };
        ajax.send("e="+e+"&p="+p);
    }
}

I am using the latest version of chrome on a 64 bit windows machine.

Update

OK guys I have verified that the response via php script is 'login_successful' and it doesn't contains any line so that we can trim on it.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Utkarsh Vishnoi
  • 149
  • 2
  • 14

4 Answers4

0

check the result by alert in between response condition & use

window.location.href='correctfilepath.php';

correct path of file to be redirected. Check your user.php also as it will not print anything if it will have some error in code

lakshman
  • 656
  • 4
  • 18
0

if you use location i prefer to use a absolute path. Maybe this is your problem and a redirect will be working with this. For example:

location.href = '/user.php';

If you want to redirect to a url do it like this:

location.href = '//www.google.com';

(it will automatically use http or https based on the current scheme.

user3714751
  • 326
  • 3
  • 15
0

Try setting the full URL for redirection

window.location = window.location.protocol + "//" + window.location.host + "/user.php"

instead of window.location = 'user.php';

Edited:

window.location = "//" + window.location.host + "/user.php"

Javascript redirection works without the protocol (http or https) also.

Valarpirai
  • 465
  • 3
  • 14
0
if(response === 'login_successful')// Check if user is logged in succesfully.
                {
                    window.location = 'user.php';//Redirect the user to the new page.(Not working)
                }
                else
                {
                    _('status').innerHTML = response;// Display the response
                    _("loginbtn").style.display = "block";// Display the login button
                }

In the else block you had given _('status').innerHTML = response; check that block is executing or not if its is executing then try to change the (=== as == )in the if condition hope it may work.