我正在尝试使用Ajax发送表单而不刷新页面.将表单提交到同一页面非常重要,这就是我使用url:’customer-management?form_sent = yes’的原因.
HTML:
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<i onclick="$(this).closest(\'form\').submit()" id="' . $get_uncontacted_member->id . '" class="fa fa-phone-square called"></i>
</form>
JS:
$('.called').click(function() {
$.ajax({
type: 'post',
url: 'customer-management?form_sent=yes',
data: $(this).attr('id');
success: function(r) {
alert(r);
}
})
})
PHP:
if (isset($_POST['form_sent']) && $_POST['form_sent'] === 'yes') { return 'That worked!' }
页面重新加载,我想我做错了什么.
解决方法:
您没有正确传递数据.由于您正在执行POST请求,因此不应将数据作为查询字符串传递.而是通过data属性传递它.另外,添加return false;这样表格就不会提交.
$.ajax({
type: 'post',
url: 'customer-management',
data: { form_sent: 'yes' },
success: function(r) {
alert(r);
}
});
return false;
onclick="$(this).closest(\'form\').submit()"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。