简体   繁体   中英

Ajax form plugin HTML response always returning error message with mysql response true

I'm creating a login script with PHP and the Jquery form plugin to process the login form. It uses this plugin to process the values to the PHP script checking the cridentials. The the PHP echoes "true" or "false" and Jquery handles the error messages or redirection to the dashboard. Here is my Jquery:

<script> 
$(document).ready(function() { 
// bind 'myForm' and provide a simple callback function 
$('#loginform').ajaxForm(function(html) { 
    success:{
        if(html == 'true'){
            window.location = "dashboard.php";
        } else if (html == 'false') {
            $(".errormsg").show();
            $(".errormsg").css({
                background: "#fc4452", 
                border: "1px solid #ff0000", 
            });                        
            $('.errormsg').html('E-mail adres of wachtwoord onjuist');
        } else {
            $(".errormsg").show();
            $(".errormsg").css({
                background: "#fc4452", 
                border: "1px solid #ff0000", 
            });                        
            $(".errormsg").html('Er ging iets mis bij het inloggen');
        }
    }
}); 
}); 

here is my current php script that searches the db:

include("../includes/mysql_connect.php");
$email = $_POST['email'];
$password = $_POST['password'];
$password = hash('sha256', $password);

 // Initialize a session:
$query_check_credentials = "SELECT * FROM Users WHERE (email='$email' AND    password='$password')";
$result_check_credentials = mysqli_query($dbc,      $query_check_credentials);
if(!$result_check_credentials){ //If the Query Fails
 echo 'Query Failed ';
 }

if(mysqli_num_rows($result_check_credentials) == 1) //if Query is successfull
 { // A match was made.
    session_start();
    $row = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);
    $_SESSION['email'] = $row['email'];
    echo "true";
  }else{
     echo"false";  
  }

As you can see it echoes true or false, which is read by the html function in the ajax script and then displays a message or does a redirect.

The problem is, it is not doing that at the moment. It is always displaying the error message in the "else" statement. Which reads: "something went wrong", even when the correct credentials are entered.

I've tested my PHP without the jquery and I know for fact that it returns "true" or "false" based on the credentials. Nothing more, no whitespace or other stuff.

To troubleshoot, I also wrote the simple piece of PHP code below, which does the same thing as the PHP above, but is not using a database. The strange thing is, when I use the PHP below...my jQuery is working fine, it shows a message when the password or username is wrong, and it shows the corresponding messages with passwords "1" and "123".

Is this because of the mysql query or am I missing something else? Thanks in advance!

PHP test script

$email = $_POST['email'];
$password = $_POST['password'];
    if($email=="john@doe.com" && $password == "123"){ 
      session_start();
      $_SESSION['email'] = $email;
      echo "true";
    } else {
      echo "false";
    }

I found the problem, In my first PHP file i'm including the file "mysql_connect.php" after a closer inspection, this was causing extra content on the PHP response. I solved it by stripping this from my "mysql_connect" file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM