0

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???

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274

4 Answers4

3
$.post(
    'include/save_legatee.inc.php', 
    { all_legattee_list: ['mohit', 'jain'] }, 
    function(data) {

    }
);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

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
  • 184,426
  • 49
  • 232
  • 263
1

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
  • 8,615
  • 10
  • 58
  • 81
  • 2
    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... – Mohit Jain Mar 05 '10 at 07:37
  • 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 Mar 05 '10 at 07:40
0

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.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628