0

Why isn't my php echo not working using jquery? If the incorrect password or credentials part is executed the login2.php file loads with the message

I want to print the Invalid Credentials or Password message in the <p> tag but it takes me to another tab that is loading the login2.php file and printing the message there.

index.html

    <form id="login-form" action="login2.php" method="POST">
            <input type="text" name="email" id="login-email" class="form-control" placeholder="E-mail">
            <br>
            <input type="password" name="password" id="login-password" class="form-control" placeholder="Password">
            <br>
            <button type="submit" name="login" id="login-btn" class="btn btn-warning">Log In</button>
            <br>
              <p class="login-msg">
 
              </p>
            <br>
            <a href="signup.php" class="text-secondary">Don't have an account? Sign up</a>
    </form>

login2.php

    <?php 
    
        include "connection.php";
    
        if(isset($_POST["login"]))
        {
            $email = $_POST["email"];
            $password = $_POST["password"];
    
            $query="select * from user where email='$email'";
            $result = mysqli_query($conn,$query);
            $row = mysqli_fetch_assoc($result);
            $num_of_rows=mysqli_num_rows($result);
            $user_id=$row["user_id"];
    
            if($num_of_rows==1)
            {
                if($password == $row["password"])
                {
                   $user_type=$row["user_type"];
                   if($user_type=="buyer")
                   {
                    $_SESSION["user_id"]=$user_id;
                    header("location:index.php");
                   }
                   else if($user_type=="seller")
                   {
                    $_SESSION["user_id"]=$user_id;
                    header("location:seller_homepage.php");
                   }
                }
                else{
                    echo "<span>Incorrect Password</span>";
                }
            }
            else{
                echo "<span>Invalid Credentials</span>";
            }
        }
    
    ?>

main.js

    $('body').on('click','#login-btn',(e)=>{
        e.preventDefault();
    
        var email = $("#login-email").val();
        var password = $("#login-email").val();
    
        $.post("login2.php",{
            email : email,
            password : password
        },(data)=>{
            $(".login-msg").html(data);
        })
    
    })
  • It sounds like maybe your AJAX call is not being executed. Have you checked the browser's Console for any errors? – ADyson Jul 21 '21 at 17:42
  • 1
    You don't seem to pass `$_POST['login']` in with your Ajax request, so wouldn't that cause problems with the PHP code not executing the useful part? – droopsnoot Jul 21 '21 at 17:51
  • You seem to be storing passwords in plain-text in your database, which is a really bad idea. While you're looking at that, you should also read up on Prepared Statements instead of sticking user-supplied strings into your query. – droopsnoot Jul 21 '21 at 17:53
  • When you do a header-redirect from code that you've called in Ajax, I wonder whether that's causing a problem? The Ajax / JQuery code is waiting for a response form your PHP code, usually provided from `echo` as you do in some cases. Does a header redirect work when the Ajax code is grabbing the output? – droopsnoot Jul 21 '21 at 17:55
  • 2
    **Never store passwords in clear text or using MD5/SHA1!** Only store password hashes created using PHP's [`password_hash()`](https://php.net/manual/en/function.password-hash.php), which you can then verify using [`password_verify()`](https://php.net/manual/en/function.password-verify.php). Take a look at this post: [How to use password_hash](https://stackoverflow.com/q/30279321/1839439) and learn more about [bcrypt & password hashing in PHP](https://stackoverflow.com/a/6337021/1839439) – Dharman Jul 21 '21 at 18:00
  • 1
    @droopsnoot Ajax won't follow a redirect header, no (since the whole purpose of AJAX is to allow the browser to stay on the same page without reloads, postbacks or redirects refreshing everything). But I get the sense we're not even at the point where the Ajax is being used yet. OP needs to add some more info, however, to clarify. – ADyson Jul 21 '21 at 18:06
  • It's because you are binding on body. Change `$('body')` to `$(document)` – Dharman Jul 21 '21 at 18:08
  • @Dharman no it'll bind ok to `body` - demo: https://jsfiddle.net/nzhb9u8d/ – ADyson Jul 22 '21 at 08:36

0 Answers0