Welcome to WebmasterWorld Guest from 54.166.178.177

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

How do I create an array and pass in $_POST

Need help pass array in $_POST

     
7:05 pm on May 14, 2004 (gmt 0)

10+ Year Member



New to PHP, been coding for a month now and need help.
I have a form I created to POST back to my original PHP page. I would like to send back an array of items and their qty's as one of the?_POST variables, rather than coding name=item01, name=item02 and name=qty01, name=qty02.

How do I create an array in my script and pass it as a variable in my Post array?

Hope this makes sense and Thanks!

8:28 pm on May 14, 2004 (gmt 0)

10+ Year Member



Basically you just stick [] after the variable name

example:
<input type="text" name="test[]" value="This ">
<input type="text" name="test[]" value="Is ">
<input type="text" name="test[]" value="an array">

will post
[test] => Array
(
[0] => this
[1] => is
[2] => an array
)

Hope that helps

8:40 pm on May 14, 2004 (gmt 0)

10+ Year Member



Thanks. I was hoping I could create the array in my script and pass it to the name= in the?_Post array. Is that possible?
10:50 pm on May 14, 2004 (gmt 0)

10+ Year Member



Can you post an attempt on the code your trying to write, i'm not quite clear on what your looking for. It is late here however so my brain could be in slowmo.

ta,
Hughie

11:47 pm on May 14, 2004 (gmt 0)

10+ Year Member


I'm not entirely certain I understand either, hughie's method is how it's normally done.

alternatively, you could "serialize" the array and pass it as one string. I use this technique for saving arrays to flat files, never tried to $_post one. should work, though.

;o)
(or

10:17 am on May 16, 2004 (gmt 0)

10+ Year Member



You could do something like

$myArray=array(
'name'=>'John',
'job'=>'Director'
);

And then to produce the HTML you would do:

foreach ($myArray as $key => $value) {
print "<input type=\"text\" name=\"myArray[$key]\" value=\"$value\">\n";
}

which would print out

<input type="text" name="myArray[name]" value="John">
<input type="text" name="myArray[job]" value="Director">

For neater code I normally write this as

foreach ($myArray as $key => $value) {
printf('<input type="text" name="myArray[%s]" value="%s">',$key,$value);
}

Does that help?

1:22 pm on May 16, 2004 (gmt 0)

10+ Year Member



Sorry, I've been out, thanks for this example this is what I need. I tried to create this but got a bit confused. Thanks to everyone who answered!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month