简体   繁体   中英

php order form, passing multiple variables

I am creating a order table. my problem I am having is with my form field for each row/record with in the table.

<input type="text" size="4" name="buy_item['2']" value="0">

I am defining each identifier by a similar syntax

buy_item[ item number ] 

my problem is when the entire form is sent to through the post request how do I know exactly which items were purchased?

It is possible their could be up to 100 different items, so the post variables can always be changing. what i know how to do is hard code each item into the buy script but i feel that it is much to in efficient and would really lack the ability to add items or remove them.

$_POST['buy_item[2]']; 

would be the equivalent to the example at the top. but what if someone bought

buy_item['99'] 

instead of buy_item['2']

If anyone can lead me towards the right direction it would be greatly appreciated.

foreach($_POST['buy_item'] as $item_id=>$amount){
  if($amount > 0){
    echo $item_id." was bought ".$amount." times";
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM