如何在ajax表单提交中提交表单名称和提交按钮名称

时间:2022-11-24 10:33:36

I am looking for some concise solution in which i am able to send form name and submit button name with form input fields. I have tries form.serializeArray() method but I am getting only input fields name inside post. then how can I send complete form data with form name and submit button name in just one line code

我正在寻找一些简洁的解决方案,在其中我可以发送表单名称和提交按钮名称与表单输入字段。我有try form.serializeArray()方法,但是我只在post中获得输入字段名。然后,如何用一个行代码发送完整的表单数据和提交按钮名称

 <form id="formid" name="formName" class="formClass" action="service.php" method="POST" >
        <input type="text" id="fname" name="fname"/>
        <input type="text" id="lname" name="lname" />
        <input type="Submit" id="SUBMIT" value="SUBMIT" name="submit"/>

    </form>

    $( "#formid" ).submit(function( event ) {
    //var array = $(this).serializeArray() ;  //  Didn't send Form name and submit button
    // var data2 = new FormData($( "#formid" )[0]); // I have also tried this



      $.ajax({
        type: $(this).attr('method'),
        url : $(this).attr('action') , 
        cache: false,
        contentType: false,
        processData: false,
        data:  formData,
        complete:function(httpResponse, status) {
        var response = httpResponse.responseText;       
        alert(response);

        }
      });


    event.preventDefault();

});

});

I want to send Form name and Submit Button Name in ajax just like it works when page reloaded after submission of form.

我想用ajax发送表单名和提交按钮名,就像表单提交后重新加载页面一样。

For more I have tried MALSUP in which I am able to send form name and submit button name in the POST data. So what would be alternative way to do this in Jquery AJAX

更多信息,我尝试了MALSUP,在其中我可以发送表单名称和提交按钮名称的邮寄数据。在Jquery AJAX中,还有什么其他的方法可以实现这一点。

1 个解决方案

#1


3  

I got my answer. Even when the page reloads after submitting the form, the form name does not go with the form data. Only the all the input elements with submit button name is sent into the form data. Even the input type="button" element name does not go into form data.

我找到了答案。即使页面在提交表单之后重新加载,表单名称也不会与表单数据一起。只有带有submit按钮名称的所有输入元素被发送到表单数据中。即使输入类型="button"元素名称也不会进入表单数据。

But with AJAX form submission when we use ('form').serialize() method OR ('form').serailizeArray() method all the form elements value with their names are serialize but SUBMIT Button value DOES NOT SERIALIZE with this. But I found a solution to send submit button NAME and VALUE with minimum lines of code.

但是,在使用('form').serialize()方法或('form'). serailizearray()方法时,使用AJAX提交表单时,所有包含名称的表单元素值都是序列化的,但是SUBMIT按钮值不会序列化。但是我找到了一个用最少的代码行发送submit按钮名称和值的解决方案。

var formdata = new FormData();
     for (var i = 0; i < $(this)[0].length; i++) {
        formdata.append($(this)[0][i].name, $(this)[0][i].value);
    }

the above code will make the array of all elements including with submit button NAME and VALUE. The benefit of this is we can check for the submit button name at SERVER SIDE and can perform the valuable operations on the basis of only one condition.

上面的代码将使所有元素的数组包括submit按钮名和值。这样做的好处是,我们可以在服务器端检查submit按钮名称,并且可以在只有一个条件的基础上执行有价值的操作。

#1


3  

I got my answer. Even when the page reloads after submitting the form, the form name does not go with the form data. Only the all the input elements with submit button name is sent into the form data. Even the input type="button" element name does not go into form data.

我找到了答案。即使页面在提交表单之后重新加载,表单名称也不会与表单数据一起。只有带有submit按钮名称的所有输入元素被发送到表单数据中。即使输入类型="button"元素名称也不会进入表单数据。

But with AJAX form submission when we use ('form').serialize() method OR ('form').serailizeArray() method all the form elements value with their names are serialize but SUBMIT Button value DOES NOT SERIALIZE with this. But I found a solution to send submit button NAME and VALUE with minimum lines of code.

但是,在使用('form').serialize()方法或('form'). serailizearray()方法时,使用AJAX提交表单时,所有包含名称的表单元素值都是序列化的,但是SUBMIT按钮值不会序列化。但是我找到了一个用最少的代码行发送submit按钮名称和值的解决方案。

var formdata = new FormData();
     for (var i = 0; i < $(this)[0].length; i++) {
        formdata.append($(this)[0][i].name, $(this)[0][i].value);
    }

the above code will make the array of all elements including with submit button NAME and VALUE. The benefit of this is we can check for the submit button name at SERVER SIDE and can perform the valuable operations on the basis of only one condition.

上面的代码将使所有元素的数组包括submit按钮名和值。这样做的好处是,我们可以在服务器端检查submit按钮名称,并且可以在只有一个条件的基础上执行有价值的操作。