views:

114

answers:

4

hello frieds this is how i usually post a variable using Jquery..

$.post("include/save_legatee.inc.php", { legatee_name: legatee_name_string,
                                            legatee_relationship:legatee_relationship_string,                                               
                                                            }, function(response){

                                                                alert(response);

                                                            });

Can anybody explain how to post an array using Jquery..

   var all_legattee_list= new Array();
  all_legattee_list[1]="mohit";
  all_legattee_list[2]="jain";

this is my array... How can post this using jquery???

A: 

you have to use all_legattee_list[] as name of your parameter, something like this:

$.post("...", "all_legattee_list[]=mohit&all_legattee_list[]=jain", ...);
Laimoncijus
dude i dont know abt the length of the array(it can be from 10, max).. i m havin 4 arrays that i have to post.. dont u thing this is insane way to do that...
piemesons
I don't see how many arrays you have, for simple few elements it's not that bad to do, for many - maybe try using 'all_legattee_list[]' as element name in params object? my example was just a tip what kind of param name you need to use so PHP would use it as array...
Laimoncijus
+1  A: 

Post this as a string separated with some delimiter.

use .join() to join the array

var strJoinedArray = all_legattee_list.join(',');

and in your php code split the string using , and retrieve the values.

rahul
Works only if array elements cannot contain the delimiter.
Tomalak
+3  A: 
$.post(
    'include/save_legatee.inc.php', 
    { all_legattee_list: ['mohit', 'jain'] }, 
    function(data) {

    }
);
Darin Dimitrov
A: 

For example, like this: Serializing to JSON in jQuery. Of course the server would have to support JSON deserialization, but many server-side languages already do.

Tomalak