如何在Ajax jQuery中正确使用return
How to use return properly in Ajax jQuery?
我的登录表单有问题。它不允许我登录到下一页。它会显示我的电子邮件和密码是否匹配,但它不允许我登录。请看一看,告诉我它有什么问题,或者建议我一些可行的方法。谢谢
HTML代码
<div id="loginForm">
<form id="formLogin" method="post" action="login.php" >
<input type="text" id="eMailTxt" placeholder="Email Address" />
<input type="password" id="passWordTxt" placeholder="password" />
<p></p>
<input type="submit" value="Login" id="submitBtn" class="Btn" />
</form>
</div>
jQuery AJAX代码
$(document).ready(function(){
$("#formLogin").submit(function(){
$a = $("#eMailTxt").val();
$b = $("#passWordTxt").val();
$.post("loginCheck.php",{
email: $a,
pass: $b
},function(data){
if (data=="false")
{
$(".loginForm p").html("Password does not match").css({'color':'red'});
return false;
}
else
{
$(".loginForm p").html("Password match").css({'color':'green'});
$("#formLogin").submit(); //I've tried return true and this submit function but none worked.
}
});
return false;
//If I turn this true, it logs in everytime even if password doesn't match. And if it's false, it will return false everytime even if password is correct
});
});
PHP代码
<?php
$q=$_POST["email"];
$s=$_POST["pass"];
$con=mysqli_connect("localhost","root","","SocialNetwork");
$check="SELECT PassWord FROM people WHERE EMAIL = '".$q."'";
$data=mysqli_query($con,$check);
$result=mysqli_fetch_array($data);
if ($s != $result[0])
{
echo "false";
}
else
{
echo "true";
}
?>
尝试这种不同的AJAX结构,它可能更适合您。
此外,我更改了您的PHP代码,只回显您发送的内容(作为测试,以验证PHP处理器文件是否接收到您期望的内容)。
从这里开始,您应该能够一次添加一个内容,并使其全部工作。
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#formLogin").submit(function(){
$a = $("#eMailTxt").val();
$b = $("#passWordTxt").val();
//alert($a +' '+ $b);
$.ajax({
type:"POST",
url: "loginCheck.php",
data: 'email=' +$a+ '&pass=' +$b,
success: function(data) {
alert(data);
}
});
return false; //This line prevents form submit -- REMOVE when done testing
});
}); //END $(document).ready()
</script>
</head>
<body>
<div id="loginForm">
<form id="formLogin" method="post" action="login.php" >
<input type="text" id="eMailTxt" placeholder="Email Address" />
<input type="password" id="passWordTxt" placeholder="password" />
<p></p>
<input type="submit" value="Login" id="submitBtn" class="Btn" />
</form>
</div>
</body>
</html>
PHP代码:logiCheck.PHP
<?php
$q=$_POST["email"];
$s=$_POST["pass"];
die('Received: ['.$q.'] and ['.$s.']'); //Remove this line when done first test
$con=mysqli_connect("localhost","root","","SocialNetwork");
$check="SELECT PassWord FROM people WHERE EMAIL = '".$q."'";
$data=mysqli_query($con,$check);
$result=mysqli_fetch_array($data);
if ($s != $result[0])
{
echo "false";
}
else
{
echo "true";
}
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- Jquery ajax return null
- Php ajax return json or html
- 为什么event.prventDefault()和'return false'停止我的AJAX表单提交操
- return ajax.done数据在jquery上出现错误
- AJAX RETURN 帮助 php/mysql 动态下拉菜单
- Ajax success: {return false;}
- Simple jquery ajax return
- Return语句在ajax调用之前调用
- Return false不适用于AJAX
- 发送ajax请求时出现return语句问题
- 如何在Ajax jQuery中正确使用return
- return request method = GET but i send request = POST (ajax)
- 第一次函数是return Undefined,每隔一次它就工作一次?使用Jquery、Javascript、Ajax
- Ajax . responsetext在alert中可用,但不会保存在变量或return中
- jquery ajax json doesnt return true
- 通过AJAX在JavaScript中生成一个变量return Value
- Angularjs ajax return not consistent
- Jquery Ajax Return
- jQuery AJAX and PHP return true/false