using $_POST to retrive dynamicly created variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
socalocmatt
Forum Newbie
Posts: 15
Joined: Tue Nov 03, 2009 12:45 pm

using $_POST to retrive dynamicly created variable

Post by socalocmatt »

So I have a page that uses a loop to create multiple forms on a page that edit data stored in a sql db. Each form is sepperated using the unique id that given in the sql db. Here is a sample of what I'm working with:

Code: Select all

 
<form action="products_edit2.php" name="updateBPI<? echo $id; ?>" id="updateBPI<? echo $id; ?>" method="post" enctype="application/x-www-form-urlencoded">
<input name="id" type="hidden" id="<? echo $id; ?>" />
   <select name="warehouse<? echo $id; ?>[]"  id="warehouse<? echo $id; ?>" size="3" multiple="multiple" >
      <option value="1">warehouse1</option>
      <option value="2">warehouse2</option>
      <option value="3">warehouse3</option>
   </select>
   <input name="Submit" type="submit" id="Submit" value="Update" />
</form>
<hr />
<form action="products_edit2.php" name="updateBPI<? echo $id; ?>" id="updateBPI<? echo $id; ?>" method="post" enctype="application/x-www-form-urlencoded">
   <select name="warehouse<? echo $id; ?>[]"  id="warehouse<? echo $id; ?>" size="3" multiple="multiple" >
      <option value="1">warehouse1</option>
      <option value="2">warehouse2</option>
      <option value="3">warehouse3</option>
   </select>
   <input name="Submit" type="submit" id="Submit" value="Update" />
</form>
 
On products_edit2.php how would I receive the information sent by warehouse<? echo $id; ?>[] ? I know that if the value name is know I can use:

Code: Select all

 
is_array ($_POST['warehouse']) ? $warehouse=implode(",",$_POST['warehouse']) : $warehouse=$_POST['warehouse'];
 
But if I am dynamically appending the unique id to the end of the name, how can this data be captured? Is there a way to use wildcards, such as:

Code: Select all

 
is_array ($_POST['warehouse%%']) ? $warehouse=implode(",",$_POST['warehouse%%']) : $warehouse=$_POST['warehouse%%'];
 
or is there a way to pass the id and use it is some way such as:

Code: Select all

 
$id=$_POST['id'];
warehouse=$_POST['warehouse$id']
 
Any help on this is very much appreciated.
:banghead:
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: using $_POST to retrive dynamicly created variable

Post by requinix »

Code: Select all

name="warehouse<? echo $id; ?>[]"
You're already using arrays - why not use more?

Code: Select all

name="warehouse[<? echo $id; ?>][]"
socalocmatt
Forum Newbie
Posts: 15
Joined: Tue Nov 03, 2009 12:45 pm

Re: using $_POST to retrive dynamicly created variable

Post by socalocmatt »

Because that would only be an obvious solution that I didn't think of.

Thanks!!! That will do the trick! :D
Post Reply