我正在尝试使用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!' }
页面重新加载了,我猜我做错了什么。
onclick="$(this).closest(\'form\').submit()"
。它会重新加载页面。 - Farzheronclick="$(this).closest(\'form\').submit()"
,而JavaScript中有$('.called').click(function() { });
,为什么两者都要用?此外,在这两个代码中都没有阻止默认表单提交。尝试在$('.called').click(function() { });
代码中添加event.preventDefault();
。 - Sean