3

so lets say I wanted to essentially do this:

$.post(
    'search_item.php', 
    { 
        serialzed_data, 
        save: form.save.value, 
        is_correct: form.is_correct.value , 
        etc...
    }
)

What is the correct syntax to do so?

many thanks,

EDIT to specify:

lets say I have this:

$.post(
    'search_item.php', 
    { 
        'checks':post_data, 
        'option[]':option, 
        save: form.save.value, 
        item: form.item.value, 
        name: form.i_name.value, 
        desc: form.i_desc.value, 
        text: form.i_text.value 
    },
    function(output)    {
        $('#return2').html(output).show();
    });

now with that current .post I want to add this to it and post them together:

var serialized_data = $('input[name^="checks"]:checked').serialize();

can I do it?

EDIT latest attempt:

var post_data = $('input[name^="checks"]:checked').serialize();

        var data = $.extend({}, post_data, {
        'option[]':option, save: form.save.value, 
        item: form.item.value, 
        name: form.i_name.value, 
        desc: form.i_desc.value, 
        text: form.i_text.value
        });

        $.post('search_item.php', data ,
        function(output)    {
            $('#return2').html(output).show();
        });
dapperwaterbuffalo
  • 2,672
  • 5
  • 35
  • 48
  • Define "serialized data". Objects in javascript ought to have name for each their property. – zerkms Jun 03 '11 at 00:54

4 Answers4

4

Assuming serialzed_data is an object of key -> value properties, use jQuery.extend(), eg

var data = $.extend({}, serialzed_data, {
    save: form.save.value,
    is_correct: form.is_correct.value,
    // etc
});

$.post('search_item.php', data, ...
Phil
  • 157,677
  • 23
  • 242
  • 245
1

I tried cakar's method and it works beautifully. Use parse_str() in the php file to get the serialized data which is passed from jquery into an array

1

You want to use serializeArray instead (.serialize turns the elements into a string, not an array) like so:

$.post('search_item.php', {
        serializedData: $('input[name^="checks"]:checked').serializeArray(),
        extraVar: value
    },
    function(output)    {
        $('#return2').html(output).show();
    });

The serializedData will be an array, not a 'dictionary', so you'll have to parse it out on the other end.

Actually... .serialize would work just fine too. If you're using PHP on the backend, you'd just have to pass it through parse_str.

or you can make an actual object of key/value pairs and go with Phil's solution using this.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
  • either! depends on your backend. I'd probably use `.serialize` because it's super easy to parse into a dictionary in most languages. – mpen Jun 03 '11 at 01:20
  • my serialized data is not coming through on the other side :S – dapperwaterbuffalo Jun 03 '11 at 01:22
  • then your problem is elsewhere. start simpler. try passing `{var:"val"}` or something and see if you can access that from your script. use firebug or chrome developer tools to check the request and response. – mpen Jun 03 '11 at 01:26
  • got my serialized data posting now, gna try that parse_str coz serializeArray() didnt seem to work. – dapperwaterbuffalo Jun 03 '11 at 01:27
  • `print_r($_POST)` and show us what you've got if you're still having trouble. – mpen Jun 03 '11 at 01:29
  • thankkkk **** for that. its not even funny how long I am been trying to do that. thank you @Mark and @Phil. – dapperwaterbuffalo Jun 03 '11 at 01:38
1
data: {
    'formData'         : $("#someForm").serialize(),
    'anotherParameter' : 'someOtherData',
     and so on .... 
},
cakar
  • 71
  • 1
  • 4