AJAX表单提交,提交另一个表单甚至考虑使用$(this)


AJAX form submission, submitting another form even thought using $(this)

我的页面上有两个表单。

一个在页脚,是一个联系人页面,但另一个是我的主表单,称为"产品表单"。我正在用PHP做一些验证:

foreach( $_POST as $field => $val ) {
    if ($val == '') {
        echo $field . "- no val <br>";
    }
}

问题是,我看到在我的页脚表单中的字段正在被验证…

这是我的AJAX:

$('body').on('submit', '.product-form', function(e){
    e.preventDefault();
    var thisForm = $(this);
    $.ajax({
        url : '/wp-content/themes/theme/page-templates/template-parts/template-logic/send-form.php',
        type : 'POST',
        data : thisForm.serialize(),
        before : $(".error-message").remove(),
        success : function(data) {
        }
    });
});

这是如何从两个表格中提交数据的?

这两种形式的标签都被适当地关闭了,也有不同的类。

这个项目必须使用WordPress,通常在家里使用Laravel验证,所以这有点倒退!

请求的表单HTML:

我想要的表格:

<form method="post" class="product-form">
    <li class="object field">
        <label for="full name">Your full name <span>*</span></label>
        <input class="req" autocomplete="off" type="text" name="full name" id="full name" placeholder="e.g. John Doe">
    </li>
</form>

页脚形式:

<form class="footer-form small">
    <li>
        <button class="object button large" type="submit">Get in touch</button>
    </li>
</form>

请这样更改:

$( ".product-form" ).submit(function( e) {
e.preventDefault();
var thisForm = $(this);
$.ajax({
    url : '/wp-content/themes/theme/page-templates/template-parts/template-logic/send-form.php',
    type : 'POST',
    data : thisForm.serialize(),
    before : $(".error-message").remove(),
    success : function(data) {
    }
});
});