jquery ajax的问题 [英] problem with jquery ajax
问题描述
嗨
我的javascript函数有问题。我有一个注册表单,其中我使用jquery ajax检查输入的用户名是否被另一个人使用(通过检查数据库)。按下提交按钮validateMyForm()-my javascript function-被调用,其中我验证其中的输入。我的jquery ajax也在其中。(你可以在下面看到我的代码)。
你知道如果validateMyForm()函数的返回值为true,则表单被发布(正常方式没有用ajax方式)。如果输入的用户名与任何一个用户名不匹配,我想发布我的表单,如果其他人使用了用户名,则不想发帖。但是当我按下提交按钮时,该功能跳过$ .post部分并始终返回true。
你能帮我吗?
我的PHP代码是:
Hi
I have a problem with my javascript function. I have a register form in which i used jquery ajax to check that entered username is used by another or not(by Checking database). after press submit button "validateMyForm()" -my javascript function- is called in which i validate inputs in it. my jquery ajax is in it too.(you can see my codes in following).
You know that if returned value of "validateMyForm()" function is true, form is posted(in normal way no with ajax way). i want to post my form if entered username don't match with any one and don't want to post if username is used by another. but when i press submit button, the function skips "$.post" section and returns true always.
Can you help me?
My PHP code is:
<form method="post" action="signup.php" önsubmit="return validateMyForm();" id="formtag">
<input id="username" name="username" class="input-text" type="text"/>
<p id="username-avil" style="display: none;color: rgb(255, 0, 71);">* Sorry, This username is
avilable!!!</p>
<input type="submit" name="submit1" id="register" value="Register" />
</form>
我的javascript函数是:
And my javascript function is:
function validateMyForm() {
var returnedVal = true;
//get username input text
.
.
.
$.post('ajax/checkUsername.php',{ username:username },function(data){
//Checking that entered username is used by another person or no
.
.
.
if(is used by another)
returnedVal= false;
else
ShowError;
}, "json");
return returnedVal;
}
推荐答案
.post部分并始终返回true。
你能帮助我吗? ?
我的PHP代码是:
.post" section and returns true always.
Can you help me?
My PHP code is:
<form method="post" action="signup.php" önsubmit="return validateMyForm();" id="formtag">
<input id="username" name="username" class="input-text" type="text"/>
<p id="username-avil" style="display: none;color: rgb(255, 0, 71);">* Sorry, This username is
avilable!!!</p>
<input type="submit" name="submit1" id="register" value="Register" />
</form>
我的javascript功能是:
And my javascript function is:
function validateMyForm() {
var returnedVal = true;
//get username input text
.
.
.
.post(' ajax / checkUsername.php' ,{username:username}, function (数据){
// 检查输入的用户名是否被其他人使用或没有
。
。
。
if (由另一个人使用)
returnedVal = false ;
else
ShowError;
}, json< /跨度>);
return returnedVal;
}
.post('ajax/checkUsername.php',{ username:username },function(data){ //Checking that entered username is used by another person or no . . . if(is used by another) returnedVal= false; else ShowError; }, "json"); return returnedVal; }
我认为代码有问题。
In validateMyForm()您将 returnedVal设置为true 和在ajax响应之后将值设置为 true / false 。
所以这里的问题是 AJAX调用是异步,所以编写的代码
I think there is a problem with the code.
In validateMyForm() you are setting returnedVal to true and after ajax response you are setting the value to true / false.
So the problem here is "AJAX call are Asynchronous", so the code written after
这篇关于jquery ajax的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!