为什么我的表格要提交两次?(不寻常的实现)

Why is my form submitting twice? (Unusual implementation)

本文关键字:两次 不寻常 实现 表格 我的 提交 为什么      更新时间:2023-09-26

所以我已经学会了这不是提交表单的最佳方式,但我仍然想知道为什么它被发送两次,即使当console.log s只触发一次,我的ajax .php文件试图设置一个$_SESSION变量为真,并检查它是否为真,在发送电子邮件之前(不太重要的是-为什么它甚至调用php文件两次开始?)

<form id="SAR_FORM">
//elements
<a href="javascript:void(0)" id="form_SUBMIT" tabindex="8">Submit</a>
</form>

js

$('#form_SUBMIT').click(function(){
    console.log('ok');
    formErrors = false;
    //error Checking
    if (!formErrors){
        var serialized = $("#SAR_FORM").serialize();
        $.post( "ajax/landing/landingForm_ajax.php", serialized, function( data ) {
            console.log('loop');
            $( "#sec4_errorRow" ).html( data );
            if ($("#sec4_errorRow").html() == '.'){
                $( "#sec4_errorRow" ).html( '<span style="color:#1eb252; font-weight:400;">Thank you for contacting us! We will be in touch shortly.</span>' );
                $( "#form_submit_holder" ).html( '<div id="thanks">Thank You!</div>' );
            }
        });
    }
}); 

在回调函数参数中传递事件,并按如下方式调用preventDefault():

...
$('#form_SUBMIT').click(function(e){
  e.preventDefault();
  ...
  ...
  ...
});
...

更新:根据下面的meagar的回应