gpt4 book ai didi

php - 使用AJAX将查询发送到php文件时,变量保持为空

转载 作者:行者123 更新时间:2023-12-03 08:46:02 26 4
gpt4 key购买 nike

我有一个表单,在提交表单之前我会进行一些AJAX错误处理,只是为了改善用户体验。我的问题是变量$user_password在整个过程中似乎都为空,因此错误处理是无关紧要的。

在以下代码中,第一个键入功能用于检查密码是否长于最小长度,第二个用于检查密码是否匹配:

$(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文件:
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不会产生任何结果。

这是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>

谁能解释为什么会这样?

最佳答案

当您编写代码时,我能够使您的代码正常工作。

我为html创建了一个空白页test.php:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<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>

<script>

$(document).ready(function() {
$("input[name=user_password]").keyup(function(){
var user_password = $("input[name=user_password]").val();
//console.log('password 1 key up'); //Check for key up.. It works
//data to server...
$.post("hub.php", {
//POST name and variable...
check_password: user_password
//places fetched information...
}, function(data, status) {
console.log(data); //Check for your data.
$("#user_password").html(data); //Data was displayed in div.
});
});
});


$(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("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);
});
});

});

</script>

注意,我使用 hub.php作为AJAX的URL。

另一件事是我正在使用Jquery 3.1.1,并且在表单之前加载了库。

我还将您的jquery代码包装在 <script>标记中。

我将 hub.php页面与 test.php放在同一文件夹中:
<?php

echo 'I made it.'; //Test to see if you landed on the page.

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!";
}
}


?>

我绝对会检查您的路径。如果您在控制台中没有看到响应,则说明您未将AJAX定向到正确的目录。

我对此进行了测试,并且可以正常工作。

关于php - 使用AJAX将查询发送到php文件时,变量保持为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53955326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com