简体   繁体   English

Ajax表单插件HTML响应始终返回错误消息,而mysql响应为true

[英]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. 我正在使用PHP和Jquery表单插件创建登录脚本来处理登录表单。 It uses this plugin to process the values to the PHP script checking the cridentials. 它使用此插件来处理PHP脚本中检查凭据的值。 The the PHP echoes "true" or "false" and Jquery handles the error messages or redirection to the dashboard. PHP回显“ true”或“ false”,并且Jquery处理错误消息或重定向到仪表板。 Here is my Jquery: 这是我的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: 这是我当前搜索db的php脚本:

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. 如您所见,它回显true或false,这由ajax脚本中的html函数读取,然后显示一条消息或进行重定向。

The problem is, it is not doing that at the moment. 问题是,目前没有这样做。 It is always displaying the error message in the "else" statement. 它总是在“ else”语句中显示错误消息。 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. 我已经在没有jquery的情况下测试了我的PHP,事实上我知道它会根据凭据返回“ true”或“ false”。 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. 为了进行故障排除,我还在下面编写了简单的PHP代码,其功能与上面的PHP相同,但未使用数据库。 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". 奇怪的是,当我在下面使用PHP时...我的jQuery运行正常,当密码或用户名错误时,它会显示一条消息,并使用密码“ 1”和“ 123”显示相应的消息。

Is this because of the mysql query or am I missing something else? 这是因为mysql查询还是我缺少其他东西了吗? Thanks in advance! 提前致谢!

PHP test script PHP测试脚本

$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. 我发现了问题,在仔细检查后,在我的第一个PHP文件中包含了“ mysql_connect.php”文件,这引起了PHP响应上的额外内容。 I solved it by stripping this from my "mysql_connect" file. 我通过从“ mysql_connect”文件中删除此文件解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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