tags:

views:

585

answers:

7

Though $.ajax() can be used to do ajax things, I don't think its fit for posting values of a big form.

How would you post a big form (many fields) without entering all them by hand?

+7  A: 

What is your reasoning behind that assumption? POST is designed for transferring larger amounts of data than GET. An AJAX POST request is almost exactly the same as a "normal" POST request, it's just bundled and handled internally by a browser in a slightly different manner. A couple of headers might be slightly different, but the data is all the same. Why should AJAX fail to handle a "large" form?

What would you even define as a "large" form anyway?

Edit: Thanks for the clarification on your question. I understand what you're asking now, and I see where you're coming from. For a form with a lot of inputs, it could be a pain to bundle it up into an Ajax request all the time.

Since you're using jQuery, there's an easy solution to this. Check out the serialize() method. You give it a form, and it gives you back a query string of all the form input elements and values which you can pass directly to an ajax request. There's an example there on the manual page that shows how it's done.

All you have to do is this:

$.ajax({
    data: $("form").serialize(),
    //etc.
});

where "form" is the id of your form.

zombat
I disagree that POST is for transferring larger amount of data then GET. POST is for altering the state of the server with new information. GET is for retrieving a resource from the server.
John F. Miller
How is serialize() done?I'm caring for its performance.
Shore
@John: Yes, that is the primary intention of each. However, there is no practical limit on the size of a POST request, whereas GET requests have known limits that cause problems when exceeded. I suppose my use of the phrase "specifically for" is mildly incorrect, as it implies that that is the *only* purpose of POST, which of course was not the intended interpretation.
zombat
@Shore - Well, it's all done client-side, and it basically does the same thing that you would otherwise have to do manually - it accesses all the form elements and concatenates their values into a string. I'm sure you won't notice a performance hit. You could always benchmark it if you were worried about it. It's definitely the right tool to answer your question though.
zombat
+1  A: 

What you're asking is not hard. All you have to do is collect the content of the form and pass it to your server (usually using JSON).

Take a look at this howto:

http://net.tutsplus.com/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/

Santi
The trouble is how to collect the content from a large form?It'll be very long to write by hand.
Shore
+1  A: 

If you haven't tried it yet. Then create a BIG form (now whatever you mean by that) and use $.ajax() or jQuery Forms plugin to post it. You will know if it is for this kind of things or not!

EDIT:- (after your edit) Then forms plugin is for you! Give it a shot.

TheVillageIdiot
Do you know how it decides post content?Automatically(some default mechanism like when we submit the form) or check one by one?
Shore
@Shore you said you have to **set data manually which is..** This will automatically submit all the fields in the form. You can serialize a subset of fields to query string. Please check their documentation here:http://malsup.com/jquery/form/
TheVillageIdiot
+1  A: 

You can use jQuery.post( url, data, callback, type), as its simpler to jQuery.ajax( options ).

By using serialize, you can send the whole form automatically.

$.post("/post_handler/",
    $("form#my_form").serialize(),
    function(json){
        /*your success code*/
    }, "json");

A more complete example:

<script>
$().ready(function(){
    $("form#my_form submit").click(function(){
        $.post("/post_handler/",
            $("form#my_form").serialize(),
            function(json){
                /*your success code*/
            }, "json");
        return false;
    });
}
</script>
<form id="my_form" action="/post_handler/" method="post">
  <input type="text" name="text1" value="my text 1" />
  <input type="text" name="text2" value="my text 2" />
  <input type="submit" name="submit_button" value="Send" />
</form>

This would override the default post, and perform it with AJAX.

voyager
What about the serialize() itself?How is it done principly?
Shore
voyager
You talk about what while I'm asking how.
Shore
+1  A: 

Try the jquery form plugin.

It probably does exactly what you need to do out of the box.

mkoryak
+1  A: 

You probably want to use serialize if you don't want to manually deal with each element.

$.ajax({
   type: "POST",
   url: "form.php",
   data: $("#form_id").serialize()
   success: function(msg) {
     alert("Form Submitted: " + msg);
   }
 });
anveo
+1  A: 

I've sent rather complex (large) forms with $.ajax() and had no issue. I have not sent files over ajax requests, but I've seen it done and it works better then traditional posts because it doesn't tie up the browser while you are uploading.

Based on your comment to @zombat, My guess it that you have a very large number of inputs, most of which are going to be blank most of the time. Two suggestions here 1) split the inputs into separate forms and only send each as soon as it is touched/completed. 2) examine the state of your form with JavaScript and wrap the information into JSON or XML, and Instead of posting the form data, post just the data structure.

"Large" should not be a problem, perhaps you can find a better adjective to describe your data that will let everyone know why it is hard to send.

John F. Miller