简体   繁体   中英

Jquery AJAX successful but no response given

I have problems with displaying the response of php in my html, if statements are not running for some reason.

The PHP echo's are not recognized by success function.

jQuery

<script>
$(document).ready(function(){
$("#login").submit(function(e){
    e.preventDefault();
    $.ajax({
        type:"POST",
        url:"logincheck.php",
        data:$("#login").serialize(),
        success:function(data){
            if(data == "Niet oke"){
                $("#status").html('Wachtwoord of username onjuist');
            }
            if(data == "Oke"){
                $("#status").html('Mooi');
            }
        }
    });
});
});
</script>

The HTML file

HTML

<form id="login">
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" name="submit" value="Submit" />
</form>
<span id="status" style="color:black"></span>

*Added the logincheck.php file, added the suggestion for header.

PHP

<?php
header("Content-type: application/json");
include_once("includes/dbcon.php");
include_once("pass_system.php");
if(!empty($_POST["username"]) && !empty($_POST["password"])){
$username = $_POST["username"];
$password = $_POST["password"];

$statement = $dbconnect->prepare("SELECT * FROM login WHERE username= :username");
$statement->execute(array(":username" => $username));
while($row = $statement->fetch()){

    $salt1 = substr($row["password"], 0, 10);
    $salt2 = substr($row["password"], -10);

    if(pwhash($password, $salt1, $salt2) === $row["password"]){
        header("location: status.php");
        echo "Oke"; 
    }
}
}else{
echo "Niet oke";
}
?>

Open your Javascript console, see what your PHP script respond to your AJAX call. If you can, I suggest you to make JSON responses, and put an header("Content-type: application/json") in your PHP script.

Is better that you remove name="submit" inside the <input type="submit" name="submit" value="Submit" /> because when you sent, sent also submit=

The tag is better that you replace with <script type="text/javascript"> .

You has loading the library of Jquery example: <script src="http://code.jquery.com/jquery-2.0.0.js"></script>

Your exercise not contain nothing error for me...

Could you alert or console.log the data response ?(you have to do)

如果响应成功,请尝试使用trim函数作为响应数据。

if($.trim(data) ==

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