javascript 使用 jQuery 检查电子邮件地址是否已存在于数据库中,并且 PHP.return false 不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13012001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if email address already exists in the database using jQuery and PHP.return false is not working?
提问by Deepu
Possible Duplicate:
jquery ajax return: undefined
可能重复:
jquery ajax 返回:未定义
I have written the following code to check email already exists or not using jquery and jquery validate.Jquery is used for email format validation. I have called ****check-email.php** file in jquery function.even though there emailaddress is present in my db,the action is get called and it directs to dashboard page.Actually it should not direct to next page because email is already exists.My **"return false;"in jquery function has no effect.It goes to next page even if it is present in db. I am not getting where I am wrong ???
我编写了以下代码来检查电子邮件是否已存在或不使用 jquery 和 jquery validate.Jquery 用于电子邮件格式验证。我在jquery 函数中调用了 ****check-email.php** 文件。即使我的数据库中存在电子邮件地址,该操作也会被调用并指向仪表板页面。实际上它不应该指向下一页,因为电子邮件已经存在。我的**“返回假;” 在 jquery 函数中没有效果。即使它存在于 db 中,它也会转到下一页。我不明白我错在哪里???
here is my code
这是我的代码
index.html:-
index.html:-
<form name="mysignupform" id="mysignupform" class="form-horizontal" action="Dashboard" method="post">
<div class="control-group">
<label class="control-label" for="inputEmail" >Email</label>
<div class="controls">
<input type="text" name="email" id="inputEmail" placeholder="Email" >
</div>
</div>
<br/>
<div class="control-group">
<label class="control-label" for="inputPassword" >Password</label>
<div class="controls">
<input class="password" type="password" name="password" id="inputPassword" placeholder="Password">
</div>
</div>
<div class="controls">
<input type="hidden" id="body" name="body" value=""></input>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" name="submit" id="signupsubmit"class="btn btn-inverse" onsubmit="return function(e)">Go to Dashboard</button>
</div>
</div>
</form>
my jquery function:-
我的 jquery 功能:-
<script>
$(document).ready(function(){ //newly added
$('#signupsubmit').click(function()
{
alert('in');
var emailVal = $('#inputEmail').val(); // assuming this is a input text field
alert(emailVal);
$.post('check-email.php', {'email' : emailVal}, function(data) {
if(data=='true') return false;
else $('#mysignupform').submit();
});
});
});
</script>
my check-email.php:-
我的 check-email.php:-
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mojiva", $con);
$sql = "SELECT email FROM user_info WHERE email =".$_POST['email'];
$select = mysql_query($sql,$con);
$row = mysql_num_rows($select);
if ($row > 0)
{
echo 'true';
}
else
{
echo 'false';
}
?>
I tried true false in jquery function immediate after $('#signupsubmit').click(function() this function at this time return true works.but when post is get called it does not have any effect of return false.
我在 $('#signupsubmit').click(function() 之后立即在 jquery 函数中尝试了 true false 这个函数返回 true 有效。但是当 post 被调用时它没有任何返回 false 的效果。
回答by William The Dev
It's probably because the return false is not referring to the form but the click event.
这可能是因为 return false 不是指表单而是单击事件。
On the form:
在表格上:
<form name="mysignupform" id="mysignupform" class="form-horizontal" action="Dashboard" method="post" onsubmit="email_check($('#inputEmail').val())" >
Then for JQuery:
然后对于 JQuery:
<script>
$(document).ready(function(){ //newly added
function check_email(emailVal){
{
alert('in');
alert(emailVal);
$.post('check-email.php', {'email' : emailVal}, function(data) {
if(data=='true') return false;
else return true;
});
}
});
</script>
回答by kwelsan
Try to change this line by change "type" from submit to button.
尝试通过将“类型”从提交更改为按钮来更改此行。
<button type="button" name="submit" id="signupsubmit" class="btn btn-inverse" onsubmit="return function(e)">Go to Dashboard</button>
回答by Ja?ck
If you want to stop the form submission, you need to prevent the click from bubbling up:
如果要停止表单提交,需要防止点击冒泡:
$('#signupsubmit').click(function(e) {
e.preventDefault();
// do your ajax here
}