Sending jQuery.ajax data simultaneous to a form submit

Posted by dscher on Stack Overflow See other posts from Stack Overflow or by dscher
Published on 2010-05-13T06:45:33Z Indexed on 2010/05/13 6:54 UTC
Read the original article Hit count: 339

Filed under:
|
|
|
|

I have a bit of a conundrum. I have a form which has numerous fields. There is one field for links where you enter a link, click an add button, and the link(using jQuery) gets added to a link_array. I want this array to be sent via the jQuery.ajax method when the form is submitted. If I send the link_array using $.ajax like this:

    $.ajax({
        type: "POST",
        url: "add_stock",
        dataType: "json",
        data: { "links": link_array }
        });

when the add link button is selected the data goes no problem to the correct place and gets put in the db correctly. If I bind the above function to the submit form button using $(#stock_form).submit(..... then the rest of the form data is sent but not the link_array.

I can obviously pass the link array back into a hidden field in HTML but then I'd have to unpack the array into comma separate values and break the comma-separated string apart in PHP. It just seems 100X easier to unpack the Javascript array in PHP without an other fuss.

So, how is it that you can send an array from javascript using $.ajax concurrent to the rest of the $_POST data in HTML?

Please note that I'm using Kohana 3.0 framework but really that shouldn't make a difference, what I want to do is add this js array to the $_POST array that is already going.

Thanks!

© Stack Overflow or respective owner

Related posts about html

Related posts about JavaScript