Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending parts of form using jQuery serialize() and AJAX

I'm trying to send parts of one form by AJAX using jQuery's serialize. The form has 16 textfields. I have 4 buttons. The button0 sends the textfields 0,1,2,3, and button1 sends the textfields 4,5,6,7, etc etc. How can I do it?

HTML

<html>  <head>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">     <title>Serialize</title>       <script type="text/javascript" src="jquery-1.9.1.min.js"></script>     </head>  <body>     <form id='miForm' class='miForm' name='miForm' action='env.php' method='POST'>      </form>  </body> </html> 

jQuery:

     $(document).ready(function(){         for(i=0;i<16;i++){             $('form').append('Campo de texto '+i+'<input type="text" id="txt'+i+'" value="Campo '+i+'" readonly="yes"/><br>');         }         for(i=0;i<4;i++){             $('form').append('<input type="button" id="butEnv'+i+'" value="Enviar'+i+'"/><br>');         }         $('form').append('<input type="button" id="butGen" value="Enviar Global"/><br>');      }); 
like image 328
jal Avatar asked Apr 21 '13 11:04

jal


People also ask

What is serialize () in AJAX?

The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

Can we submit form using AJAX?

We can submit a form by ajax using submit button and by mentioning the values of the following parameters. type: It is used to specify the type of request. url: It is used to specify the URL to send the request to. data: It is used to specify data to be sent to the server.

Which jQuery method is used to serialize a JavaScript object to a string?

Use param() method to serialize the object element as query string and store it into a variable.

How to insert jQuery Ajax form submit using serializing?

Our next step is to insert Jquery Ajax Form Submit with Serializing because our article title is Asp.Net MVC jQuery AJAX Form Submit using Serialize Form Data into Model. Switch back to CREATE.CSHTML file to insert AJAX code. //Serialize the form datas. You can see in the above form where we enter data.

How to use jQuery serializearray() method to send Ajax request?

This is used to send Ajax request by creating serialize data on click or load event. This way, you don't need to get one by one input value and send it to Ajax request. Below example output the query string data of form fields when button clicks. In another way, jQuery serializeArray () method used to creates an array of objects from field values.

How to serialize form data into JSON string in jQuery?

The jQuery serializeArray () method will create object into array and encoded as a JSON string.You can use this method to send form data into serialize key value pair. The request serialize data will be:

How to send Form data without reloading the page in jQuery?

If you will be using jQuery’s Ajax Form Submit, you can send the form data to the server without reloading the entire page. This will update portions of a web page – without reloading the entire page. How to add extra fields or data with Form data in jQuery ajax?


1 Answers

If you really want to stay with only one form try something like I did in this fiddle.

Create sub parts for your form.

<form>     <div id="first">         <input name="tbox1" type="text">         <input name="tbox2" type="text">         <input name="tbox3" type="text">             <input id="button1" type="button" value="button1">       </div>     <div id="second">         <input name="tbox4" type="text">         <input name="tbox5" type="text">         <input name="tbox6" type="text">             <input id="button2" type="button" value="button2">       </div> </form> 

And then just select all the elements of the parts:

$(document).ready(function() {     $('#button1').on('click', function() {         alert($('#first input').serialize());     });        $('#button2').on('click', function() {         alert($('#second input').serialize());     }); }); 

Of course if you also have select boxes you have to add them to the selectors. For example:

$('#second input, #second select').serialize() 
like image 165
Marcel Gwerder Avatar answered Sep 21 '22 17:09

Marcel Gwerder