user785160
user785160

Reputation:

Adding values to serialised Array Jquery

Im trying to add data to a preserialised array. Its not working. Anyone spot why?

$("#show-friends").live("click", function() {
    var friendSelector = $("#jfmfs-container").data('jfmfs');   
    var sendArr = $(friendSelector).serializeArray();


    sendArr.push({ name: "userid", value: "<?php echo $userId;?>" });
    sendArr.push({ name: "fid", value: "<?php echo $fid;?>" });
    $.post({
         url:'test.php', 
         data: sendArr, 
         });
});  

Edit: I changed my code to:

 $("#show-friends").live("click", function() {
    var friendSelector = $("#jfmfs-container").data('jfmfs');  
    var sendArr = friendSelector.serializeArray();
        $.post({
             url:'test.php',
             data: "name=<?php echo $userId;?>&fid=<?php echo $fid;?>&jsondata="+sendArr,
             });
});  

However I get a error friendSelector.serializeArray is not a function

Upvotes: 0

Views: 124

Answers (1)

gen_Eric
gen_Eric

Reputation: 227240

Your $post call is incorrect. You don't pass an object like you would for $.ajax. You pass the url, then the data (if any), and then a callback (if you want).

It should be:

$.post('test.php', sendArr);

Or with $.ajax:

$.ajax({
    url:'test.php', 
    data: sendArr,
    type: 'POST'
});

EDIT: When you do $("#jfmfs-container").data('jfmfs'), this is return you the value of the jfmfs data. friendSelector.serializeArray doesn't work, because friendSelector is whatever the data was, not a jQuery object.

Upvotes: 1

Related Questions