gpt4 book ai didi

php - JQuery 和 PHP 验证问题?

转载 作者:行者123 更新时间:2023-12-01 08:27:44 24 4
gpt4 key购买 nike

我想在我的 PHP 端进行验证,然后让我的 JQuery 代码在单击提交按钮时显示您的更改已保存,但 JQuery 代码表明更改已保存,甚至当验证失败时。

我如何解决这个问题,以便 PHP 可以执行验证,然后 JQuery 可以在 PHP 完成验证后执行其操作?

这是我的 Jquery 代码。

$(function() {
$('#changes-saved').hide();
$('.save-button').click(function() {
$.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(html) {
$('div.contact-info-form').html(html);
$('#changes-saved').hide();
$('#changes-saved').html('Your changes have been saved!').fadeIn(4000).show();
});

$('a').click(function () {
$('#changes-saved').empty();
$('#changes-saved').hide();
});

return false; // prevent normal submit
});
});

这是我的 PHP 代码的一部分。

// Check for an email address:
if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $_POST['email'])) {
$email = mysqli_real_escape_string($mysqli, strip_tags($_POST['email']));
} else {
echo '<p class="error">Please enter a valid email address!</p>';
}

最佳答案

在jquery中你可以添加一个if语句来检查php验证是否通过,在 php 中,您需要返回一个值,例如 1\0 或 true\false。

并在jquery中检查此参数

我添加了使用 json 的示例,但问题相同

jquery:

        $.post($('#contact-form').attr('action'), $('#contact-form').serialize(), function(data_pack){
if(data_pack.msg ==1){
# success do something ....
.........
}

alert(data_pack.html);

}, 'json');

php 代码如下:

if($validation_ok){
$arr = array('msg'=>1,'html'=>$html);
}
else {
$arr = array('msg'=>0,'html'=>$error_msg);
}
echo json_encode($arr);
exit;

关于php - JQuery 和 PHP 验证问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2505863/

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