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

4 Answers 4

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

    }
);
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Works only if array elements cannot contain the delimiter.
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", ...);

2 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.