gpt4 book ai didi

javascript - 使用回调将 PHP 变量传递给 JavaScript

转载 作者:行者123 更新时间:2023-12-02 14:51:32 25 4
gpt4 key购买 nike

我想在 Ajax 函数的帮助下通过 JavaScript 验证密码。

如果成功,我想传回变量( bool 值、true 或 false)并根据回调在我的 PHP 文件中执行某些操作。

但这行不通。这是我的代码:

PHP 文件:update.php

<input href="javascript:void(0);" role="button" ype="submit" value="Submit" onclick="ValidatePassword()>'

JAVASCRIPT:ValidatePassword()

在我的 Javascript 函数中,我使用此 ajax 调用检查密码,如果成功,它应该将结果回调到 php 函数。

 $.ajax({
type: "POST",
url: "checkpw.php",
data: dataString,
cache: false,
success: function(response)
{
if (result != -1 )
{
$("#passwd").val('');

// RETURN TO PHP FILE update.php -> PW IS VALID
} else {
// RETURN TO PHP FILE update.php -> PW IS INVALID
}
}
});

PHP 文件:update.php

现在我想在 php 函数中使用回调,例如:

<?php

if (passwordCallback == true)
...
else
...

?>

我应该在 ajax success 函数中做什么才能将值返回到我的 php 文件?

最佳答案

正如我在评论中所建议的,如果编码不正确,可能会导致安全问题。如果编码正确,那么当只需要执行一次时,它最终会执行两次密码检查。

相反,你可以做的是:

 $.ajax({
type: "POST",
url: "checkandupdate.php", //Combination of both
data: dataString,
cache: false,
success: function(response)
{
if (result != -1 ) {
$("#passwd").val('');
}
}
});

文件 checkandupdate.php

<?php
require "checkpw.php"; // Or whatever you need to do to validate the password
// At this point "checkpw.php" determined if the password is valid and(ideally) you can check the outcome
//Assume we store the outcome of the check in $passwordIsValid as a boolean indicating success
if ($passwordIsValid) {
//Do whatever you need to do when the password is valid
echo "1"
}
else {
// Do whatever you need to do when the password is invalid
echo "-1";
}
?>

关于javascript - 使用回调将 PHP 变量传递给 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150031/

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