[英]Return value from ajax form
I have a login form in php. 我在php中有一个登录表单。 I have used ajaxform so that the page does not get reloaded when the username or password is wrong.
我使用了ajaxform,以便在用户名或密码错误时不会重新加载页面。 I have another page checklogin.php which checks whether the username and pw is there in the database and it returns the count of the number of rows.
我还有另一个页面checklogin.php,它检查数据库中是否存在用户名和密码,并返回行数的计数。 I want to compare the count in the login.php and display the error message if the count=1 and redirect to another page if count=2.
我想比较login.php中的计数,如果count = 1则显示错误消息,如果count = 2则重定向到另一页。 I tried to display the count in an errordiv using target: 'errordiv' and checking its innerHTML but it failed to do so.
我试图使用target:'errordiv'并在errordiv中显示计数,并检查其innerHTML,但这样做失败。
My login.php 我的login.php
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<script type="text/javascript" src="scripts/jquery.form.js"></script>
<!--Slider-in icons-->
<script type="text/javascript">
function checklogin()
{
var request=$("#login-form").ajaxForm(
{
target:'#errordiv'
}).abort();
request.submit();
var divmsg=document.getElementById("errordiv");
if (divmsg.childNodes[0].nodeValue == "1")
{
divmsg.childNodesp[0].nodeValue="Sorry";
alert ("Invalid username or password");} //I didn't get the alert as well a the divmsg content didn't change
};
</script>
</head>
<body>
<!--WRAPPER-->
<div id="wrapper">
<!--LOGIN FORM-->
<form name="login-form" id="login-form" class="login-form" action="checklogin.php" method="post">
<!--CONTENT-->
<div class="content">
<!--USERNAME--><input name="un" type="text" class="input username" placeholder="Username" onfocus="this.value=''" required="required" /><!--END USERNAME-->
<!--PASSWORD--><input name="pw" type="password" class="input password" placeholder="Password" onfocus="this.value=''" required="required" /><!--END PASSWORD-->
</div>
<!--END CONTENT-->
<!--FOOTER-->
<div class="footer">
<!--LOGIN BUTTON--><input type="submit" name="submit" value="Login" class="button" onclick="checklogin()" /><!--END LOGIN BUTTON-->
</div>
<!--END FOOTER-->
</form>
<div id="errordiv" class="errordiv"></div>
<!--END LOGIN FORM-->
checklogin.php checklogin.php
<?php
include ('dbconn.php');
$user = trim($_POST['un']);
$pass = trim($_POST['pw']);
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$result=mysql_query("select Id from login where Username='$user' and Password='$pass'") or die (mysql_error());
$result= mysql_fetch_assoc($result);
$num= count ($result);
echo $num;
?>
How can I get the value of $num in login.php without displaying it to a div and compare the value of $num and accordingly display the errormsg in errordiv or if $num==2 redirect to another page. 如何在login.php中获取$ num的值而不将其显示到div并比较$ num的值,并相应地在errordiv中显示errormsg,或者如果$ num == 2重定向到另一页。
Change your code to include the code which change the div and provide alert to be included in the callback. 更改您的代码以包括更改div并提供要包含在回调中的警报的代码。
var request=$("#login-form").ajaxForm(
{
target:'#errordiv',
success: function (html) {
if ($("#errordiv).html() == "1") {
$("#errordiv).html("Sorry");
alert ("Invalid username or password");
}
}
}).abort();
request.submit();
Maybe try to make the checking request asynchronously and process user feedback in AJAX callback: 也许尝试异步发出检查请求并在AJAX回调中处理用户反馈:
JS: JS:
$(function(){
$("#login-form").ajaxForm(function(response){
if(response.count===1){
$("#errordiv").html("msg you want to show");
}else if(response.count===2){
//redirect
}
});
});
php: 的PHP:
<?php
header("Content-Type: application/json");
//your database check logic
$user = trim($_POST['un']);
$pass = trim($_POST['pw']);
$user = mysql_real_escape_string($user);
$pass = mysql_real_escape_string($pass);
$result=mysql_query("select Id from login where Username='$user' and Password='$pass'") or die (mysql_error());
$result= mysql_fetch_assoc($result);
$num= count ($result);
//and return response in JSON
echo json_encode(array("count"=>$num));
?> ?>
Hope this is helpful for you. 希望这对您有帮助。
[EDIT] [编辑]
remove the form submit button inline JS function invoking: 删除表单提交按钮内联JS函数调用:
onclick="checklogin()"
and put checklogin function logic to document ready callback, initialize the form when the DOM is ready 并将checklogin函数逻辑记录到文档就绪回调中,在DOM准备就绪时初始化表单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.