Jump to content

Passing An Array As Request Parameter To PHP


arpit_gadle

Recommended Posts

thnx for replying...

      wat i m asking is

             

 var array=new array()
                array[0]="First";
                array[1]="Second";

 

and want to pass this as single(array) request parameter to PHP script.

 

The easiest way is still going to be a URL, although you'll need to construct the parameters from your array.  Using your example:

http://host.com/script.php?array[]=First&array[]=Second

 

In your PHP script, $_GET['array'] would then be a PHP array, containing the values "First," and "Second."

 

Please note that it doesn't need to be named "array."  You can name it whatever you like.  The important thing is the [] at the end of your URL parameters.

Link to comment
Share on other sites

The thing about GET parameters is they are not (afaik) capable of multi-dimensional values, such as arrays. You could potentially use a cookie, or store in sessions, or a database, but only the later is actually capable of handling an array as an array, and not as just values that can later be constructed into an array.

Link to comment
Share on other sites

The thing about GET parameters is they are not (afaik) capable of multi-dimensional values, such as arrays.

 

um yeah you can eg

 

?foo[]=test&foo[bar]=baz&foo[bar][baz]=foo

 

Array

(

[0] => test

[bar] => Array

(

[baz] => foo

)

 

)

 

Also, you could send the javascript object as a json string and then decode it server side rather quickly and without having to loop through the array in javascript to construct the proper url

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.