Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Post: Appending data to post

Tags:

jquery

post

I'm using jQuery's post on a WordPress page.

var thename = jQuery("input#name").val();
    jQuery.post(the_ajax_script.ajaxurl, jQuery("#theForm").serialize(),
    function(response_from_the_action_function){
    jQuery("#response_area").html(response_from_the_action_function);
});

It posts the selections made in a form. Is it possible to append data to a post. So in addition to the form data, I need a couple lat longs added to the jQuery post. How can I do that. Is it possible?

Thnak you.

-Laxmidi

like image 755
Laxmidi Avatar asked Sep 28 '11 17:09

Laxmidi


1 Answers

.post() takes a string as the second argument, so you can concatenate a custom string using + followed by your string.

var thename = jQuery("input#name").val();
    jQuery.post(the_ajax_script.ajaxurl, jQuery("#theForm").serialize() + "&foo=bar",
    function(response_from_the_action_function){
    jQuery("#response_area").html(response_from_the_action_function);
});

Do be careful, though; the string has to be correctly formatted as a URL, so you need key=value pairs separated by &s.

In the example above, you'll see + "&foo=bar. The first & finishes the last value created by .serialize(), then foo= is a key, followed by bar as the value.

If you want to add more values afterwards, you can do something like this:

&foo=bar&baz=zip
like image 67
Bojangles Avatar answered Nov 01 '22 03:11

Bojangles