homepage Welcome to WebmasterWorld Guest from 54.211.68.132
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How do I create an array and pass in $_POST
Need help pass array in $_POST
lucyjaja




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

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!

 

hughie




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

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

lucyjaja




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

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?

hughie




msg:1278475
 10:50 pm on May 14, 2004 (gmt 0)

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

corz




msg:1278476
 11:47 pm on May 14, 2004 (gmt 0)
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

Netizen




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

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?

lucyjaja




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

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved