【发布时间】:2015-05-16 01:41:21
【问题描述】:
我正在尝试使用 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()"。那就是重新加载页面。 -
@StephenBugsKamenar 真的,谢谢!但我仍然没有收到警报。
-
您的 html 中有
onclick="$(this).closest(\'form\').submit()"和 javascript 中有$('.called').click(function() { });?为什么两者都有?此外,在任何一个中,您都不会阻止默认表单提交。尝试在您的$('.called').click(function() { });代码中添加event.preventDefault(); -
我相信你只是想使用 AJAX 这里是一个例子stackoverflow.com/questions/16323360/…
标签: javascript php jquery html ajax