使用AJAX将查询发送到php文件时,变量保持为空 [英] Variable remains empty when using AJAX in sending query to php file
问题描述
我有一个表单,在提交表单之前,我会进行一些AJAX错误处理,只是为了改善用户体验。我的问题是变量 $ user_password
在整个过程中似乎都为空,因此错误处理无关紧要。
I have a form in which I do some AJAX error handling before the form is submitted, just to improve user experience. My problem is that the variable $user_password
seems to remain empty throughout the process, thus the error handling is irrelevant.
在下面的代码中,第一个键入功能用于检查密码是否长于最小长度,第二个用于检查密码是否匹配:
In the following code, the first keyup function is to check if the password is longer than the minimum length and the second is to check if the passwords match:
$(document).ready(function() {
$("input[name=user_password]").keyup(function(){
var user_password = $("input[name=user_password]").val();
//data to server...
$.post("../server/hub.php", {
//POST name and variable...
check_password: user_password
//places fetched information...
}, function(data, status) {
$("#user_password").html(data);
});
});
});
$(document).ready(function() {
$("input[name=user_password_2]").keyup(function(){
var user_password = $("input[name=user_password]").val();
var user_password_2 = $("input[name=user_password_2]").val();
//data to server...
$.post("../server/hub.php", {
//POST name and variable...
password_match: user_password,
password_match_2: user_password_2
//places fetched information...
}, function(data, status) {
$("#user_password_2").html(data);
});
});
});
变量被重定向到实际执行错误处理的php文件:
The variables are redirected to a php file where the error handling is actually conducted:
if (isset($_POST['check_password'])) {
$user_password = $_POST['check_password'];
echo $user_password;
if ($user_password == "") {
echo "";
} elseif (strlen($user_password) < 6) {
echo "Password must contain at least six characters!";
} else {
echo "You are good to go!";
}
}
if (isset($_POST['password_match'])) {
$user_password = $_POST['password_match'];
$user_password_2 = $_POST['password_match_2'];
if ($user_password_2 == "") {
echo "";
} elseif ($user_password !== $user_password_2) {
echo "Sorry, passwords do not match!";
} else {
echo "You are good to go!";
}
}
尽管返回到html文件的数据仍然为空,并且回显 $ user_password
不会产生结果。
Although the data returned to the html file remains empty and echoing the $user_password
yields no result.
以下是html段:
<form action="../server/register.php" method="POST">
<div class="input_table">
<table>
<thead>
<tr>
<th><h1>Register to LIMS</h1></th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="password" name="user_password" placeholder="Select Password"><p id="user_password"></p></td>
</tr>
<tr>
<td><input type="password" name="user_password_2" placeholder="Repeat Password"><p id="user_password_2"></p></td>
</tr>
</tbody>
</table>
</div>
<button type="submit" name="user_register" class="button_1">Register</button>
<button type="button" class="button_3">Cancel</button>
</form>
有人可以解释为什么会这样吗?
Can anyone please explain why this is occurring?
推荐答案
因此,问题的答案非常简单,但很容易遗漏。这再次表明为什么编写成功的网站必须要有良好的编码习惯。我似乎在这种情况下, name = user_password
在代码的其他位置重复了,因此在使用以下命令调用时:
So the answer to the problem is quite simple but very easy to miss. Again this shows why good coding practice is imperative for writing a successful website. I seems that in this case the name=user_password
was repeated somewhere else in the code, thus when called using:
var user_password = $("input[name=user_password]").val();
它是指代码另一部分中的输入,而不是我尝试应用的输入编码。只需将html代码中的 name = user_password
更改为 name = user_password_1
,然后更改ajax代码即可:
it refered to input in another part of the code and not the input that I was trying to apply the code to. Simply by changing name=user_password
to name=user_password_1
in the html code and then changing the ajax code:
$(document).ready(function() {
$("input[name=user_password_1]").keyup(function(){
var user_password_1 = $("input[name=user_password_1]").val();
//data to server...
$.post("../server/hub.php", {
//POST name and variable...
check_password: user_password_1
//places fetched information...
}, function(data, status) {
$("#user_password_1").html(data);
});
});
$("input[name=user_password_2]").keyup(function(){
var user_password_1 = $("input[name=user_password_1]").val();
var user_password_2 = $("input[name=user_password_2]").val();
//data to server...
$.post("../server/hub.php", {
//POST name and variable...
password_match_1: user_password_1,
password_match_2: user_password_2
//places fetched information...
}, function(data, status) {
$("#user_password_2").html(data);
});
});
});
代码完美运行。尽管我个人认为这是一个愚蠢的错误,但我仍然相信将其作为答案发布很重要,这样其他人可以从仔细检查其代码中的此类愚蠢错误中受益。
The code works perfectly. Although this was, in my own opinion, an idiotic mistake, I still believe it to be important to post it as an answer so that other people may benefit from double checking their code for silly mistakes such as this.
这篇关于使用AJAX将查询发送到php文件时,变量保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!