作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一些指导,因为我真的快要疯了。
我用 php/js/ajax 创建代码来检查提交时电子邮件地址是否存在。
因此我测试了 ajax 并返回了正确的值。 (通过 Firebug 检查)
Either 1 for user exists
or 2 for user doe not exist.
现在我添加了一个警告框来检查值,并且由于某些奇怪的原因它总是显示值 1!(即使 ajax 值为 2)
function validateForm() {
var x3=document.forms["newuser"]["email"].value;
//Check if username exists.
$.post(
"http://...check_user_exists.php",
{
x3 : x3 //user email
},
function(data) {
email_check = parseInt((data.email_exists),10);
}
);
//Additional Checks
if (email_check = 1) {
$("#form_status").fadeIn("slow");
$("#form_status").text("Email Address Taken.");
alert(email_check);
return false;
}
if (email_check = 2) {
$("#form_status").fadeIn("slow");
$("#form_status").text("Email ok.");
alert(email_check);
return true;
}
more validation checks....
大家好,再次...感谢您的指导。首先,我想道歉,因为我对此很陌生......我稍微前进了一步,但停留在最后一部分,不确定如何继续。
因此表单现在验证表单元素并检查电子邮件以查看它是否存在。我剩下的唯一问题是如果电子邮件值没问题,则表单不会处理。有没有办法通过 ajax 回调函数将提交值设置为 true?
<form name="newuser" id="form" method="post" action="do_new_user.php" onsubmit="return validateForm()">
function validateForm()
{
var x=document.forms["newuser"]["name"].value;
var x2=document.forms["newuser"]["surname"].value;
var x3=document.forms["newuser"]["email"].value;
var x4=document.forms["newuser"]["password1"].value;
var x5=document.forms["newuser"]["password2"].value;
if (x==null || x=="")
{
$("#form_status").fadeIn("slow");
$("#form_status").text("Please enter your name.");
return false;
}
//more validation.....
//Check if username exists.
$.post("http://ryangosden.com/breadcrumbs/check_user_exists.php",
{
x3 : x3
} ,
function(data)
{
var obj = jQuery.parseJSON(data);
if (obj.email_exists == 1)
{
$("#form_status").fadeIn("slow");
$("#form_status").text("Email Address Taken.");
}
if (obj.email_exists == 2)
{
$("#form_status").fadeIn("slow");
$("#form_status").text("Email ok.");
validateForm(true);
}
});
return false;
}
再次感谢大家。
最佳答案
您不是在比较而是将 1
分配给 email_check
if (email_check = 1)
但应该是
if (email_check == 1)
= is an assignment operator
== is a comparison operator
=== is a strict comparison operator
关于Javascript/Ajax/PHP - 小问题让我抓狂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21685413/
我是一名优秀的程序员,十分优秀!