Cannot Redirect when Logging in an Account using javascript and php

javascript redirect
symfony redirect after login
how to redirect to another page in javascript on button click
how to redirect to another page in html after login
javascript redirect to relative url
symfony login redirect loop
javascript redirect on page load
how to redirect to another page in javascript with parameters

Currently, I am developing a website, for my own purposes. I am a beginner at web developing so please understand me if I am not getting this correct. Please Help.

I have a code for javascript when clicking an html element button for logging in. see code below:

        var username = document.getElementById("username").value;
        var pword = document.getElementById("password").value;          

function validateUser(user,pass){
    var username =user;
    var pword =pass;
    var datasend = "username="+ username + "&password=" + pword;

        crossDomain: true,
            alert("Hello World"); //Trying to pop up

I successfully triggered the button for the second time I try to click it, and the hello world message will pop out, but it cannot redirect the page if it was successfully logged in using an account in MySQL in WAMP server. Here is the code in PHP below:

// require("php/config.php");

if($_POST['username'] != '' && $_POST['password'] !='') {
    // username and password sent from form 
    echo $myusername = mysqli_real_escape_string($db,$_POST['username']);
    echo $mypassword = mysqli_real_escape_string($db,$_POST['password']); 

    //$sql = "SELECT user_id  FROM user WHERE username = '$myusername' and password = '$mypassword'";
    $sql = "SELECT * FROM user WHERE username = '$myusername' and password = '$mypassword'";
    $result = mysqli_query($db,$sql);
    $rows = mysqli_fetch_array($result);
    $count = mysqli_num_rows($result);

    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count == 1) {
        $_SESSION['login_user'] = $myusername; 
        header("Location: index.html");

    } else {
        echo '<script src=""></script>';
        echo '<script type="text/javascript">';
        echo 'setTimeout(function () { swal("Oops!","Your Account Credentials is Invalid, Please Try Again!","error");';
        echo '}, 100);</script>';

The problem is, the page does not redirect to the index.html even when the second click triggers the HELLO WORLD alert.

I don't know what I am doing wrong.

I don't why this is not working, I see the console, there is no error exist.

can someone help me with this? any help will much be appreciated. thanks and regards.

If you'd like to keep to using the AJAX setup you have at the moment (which is totally fine), what you'll need to do is to beef up the on-success function to read the returned results from your PHP.

A simple example

Instead of

header("Location: index.html");


echo "index.html"; exit;

and then add the following to your on-success function:

window.location.href = msg;

That will start to give you a flavour of how the relationship between an AJAX call and your PHP server should look like.

Next steps

  1. Instead of having your AJAX return a string (index.html) have it return an array of information, perhaps you want to welcome the user with a personalised message?
  2. You don't need to create a string (var datasend = "username="+ username + "&password=" + pword;) and feed that to your AJAX call, you can send an array.
  3. Make sure your passwords are not stored in plain text on the server.

Using the form_login Authentication Provider (Symfony Docs), Symfony comes with a built-in form_login system that handles a login form POST also be route names (but cannot have mandatory wildcards - e.g. /login/{foo} where foo has the authentication process, it will redirect the user to the login form /login . Learn Symfony � Learn PHP � Learn JavaScript � Learn Drupal � Learn� Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. w3schools .com THE WORLD'S LARGEST WEB DEVELOPER SITE

An ajax request will not follow the Location header of responses to redirect users. You need to either redirect the user manually using JS in the success callback or change your form submission to use a classic HTML form

How To Redirect to Another Webpage, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� Using JavaScript, logging to console is as simple as this: console.log("Message here"); In this article, I will show you how to log to console within PHP, why logging in PHP can be a good thing, and how it’s just as easy as logging to console using JavaScript.

The first thing to make sure, PHP Redirect Header doesn't work when sending an Ajax Request. So the solution to your problem is:

Change this part of your code in PHP file:

header("Location: index.html");


echo "Success";

And then in your Ajax Request Success Callback:

    if (msg == 'Success') {
        window.location = "/path/to/redirect";

Let me know if you have still confusion in this.

Redirect on successful login php, Logout will work without any problem On your custom login page you will have to Net, AJAX, jQuery Upon successful login, the server does not redirect the page Select “ Redirect user ” and provide the URL you want to redirect users after� The best advantages of using Google JavaScript API library is that the login process can be implemented on a single page without page refresh. In this tutorial, we will show you how to integrate login with Google account using JavaScript API and store the profile data in the database using jQuery, Ajax, PHP, and MySQL.

[PDF] How to Build a Secure Login, Login Page. • Login Redirect. • Logged In. • Log Out. – Attacks and defenses on each step User-Agent: Mozilla/5.0 (X11; U; Lin Accept: text/html GET mail/ inbox.php?email_id=11&action=mark_as_read HTTP/1.1. 2. which includes a javascript or meta Login and Authentication can't be easily segregated from the . H ow do I redirect with PHP script? How can I use a PHP script to redirect a user from the url they entered to a different web page/url? Under PHP you need to use header() to send a raw HTTP header. Using headers() method, you can easily transferred to the new page without having to click a link to continue. This is also useful for search engines.

How to go previous page after login? - PHP, But the problem is,after log in the user,the page redirects to user home page. I want if the user is logged in (by prompting that please log in to edit) from the edit � Note: In the code, given above, we are redirecting the user to login.aspx page with the URL query string with the value of the clicked page. In Login.aspx, when the user successfully logs in, we have to check the URL and redirect to the clicked page.

Redirect to referring page after logging in, B) Redirect to the wp-login.php with the redirect_to parameter set: Notice that the call to wp_redirect() This will redirect the user to referrer page, after login. // Redirect to login page redirect ('/user_authentication/'); } } Models (application/models/) User.php The User model is used to insert/update data in the users table. __construct() – Define the database table name and primary key. checkUser() – Insert or update the user account data in the database based on the OAuth provider and ID. <?php

  • Never store passwords in clear text!. Only store password hashes! Use PHP's password_hash() and password_verify() . If you're running a PHP version lower than 5.5 (which I really hope you aren't), you can use the password_compat library to get the same functionallity.
  • There are more issues here. First off, you have an answer that explains why the user isn't redirected. However, even if you would post the form normally (not with ajax), you would still have issues. For some reason, you're echoing the username and password when you escape them. If you do that, you won't be able to use header() after since headers needs to be sent before any output at all.
  • is it more safe to use just php sir?
  • @BooLaTeh, nah, AJAX is as "safe" as anything else.
  • A post is a post. No difference if you do it with Ajax or not. However, using Ajax usually makes better UX since the page don't need to reload on error.
  • Hi Sir! Can I ask a question?
  • I have an error on the php code, and it says, undefined index. which is the username and password, how to fix it sir?? I have see the solution, isset but it is not working as well
  • I have it now sir. Thanks By the way!