-2

I need to store all the items selected from a dropdown box inside array. I have only one form now with the select dropdown list. So each time I select an item from the list and submit the form it overwrites the previous item.

How do I make it work like each time it submits the id will be stored so that I can display all the items selected?

//this is the select dropdown for addon item
<select name="addon">
                       <?php
                       mysql_select_db($database_bumi_conn, $bumi_conn);
                       $query="SELECT * FROM tbl_addons WHERE status=1";
                       $result=mysql_query($query)or die(mysql_error());
                       while($row=mysql_fetch_array($result))
                       {
                           $a_id=$row['addOns_id'];
                           $a=$row['addOns'];

                       ?>

                       <option value="<?php echo $a_id?>"><?php echo $a;?></option>
                       <?php
                       }
                       ?>

                       </select>
//And this is how I store the id 
$addon_id=$_POST['addon'];


//edited with session
$_SESSION['option']=array();
$_SESSION['option'][$addon_id]=array('qty'=>$qty,'date_1'=>$date_1,'date_2'=>$date_2);
    print_r($_SESSION['option']);
    foreach($_SESSION['option'] as $option=>$value)
    {
        echo $option.'=>';
        foreach($value as $val)
        {
            echo $val;
        }
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Phil Aug 14 '14 at 05:53
  • 1
    Instead of selecting one by one,do the multi select dropdown to select multiple options and name it as `addon[]` – GautamD31 Aug 14 '14 at 05:54
  • @Phil, I have no option to use PDO although I prefer it because my boss wants it to be mysql_ – Kalaivani Nair Aug 14 '14 at 05:55
  • @Gautam3164 it must be choosen one by one,coz there's date pickup and return for each item. – Kalaivani Nair Aug 14 '14 at 05:56
  • 1
    @veronicageorge That doesn't make any sense. Is your boss an idiot? – Phil Aug 14 '14 at 05:56
  • @Gautam3164 I think I know where the problem is, it's the same form I use again and again ,so it refreshes .Thats why no item is added instead overwrites..But you have any idea how to solve it please? – Kalaivani Nair Aug 14 '14 at 05:58
  • you can use session then.. – GautamD31 Aug 14 '14 at 05:58
  • @Phil, well I guess my senior is coz he's the one telling my Boss not to come away from mysql_.I've tried to explain all the reason but he doesn't listen. – Kalaivani Nair Aug 14 '14 at 05:59
  • @veronicageorge What's his motivation for not writing new code in a modern library (*mysqli* or PDO)? I'm genuinely curious – Phil Aug 14 '14 at 06:04
  • @Gautam3164, can you look at the edited part with session. I did in that way, but still I'm not getting array of values but only single value. – Kalaivani Nair Aug 14 '14 at 06:04
  • @Phil I was curious too, guess what I asked him, and he said it's very complicated to handle when it needs some modification. he said it will take weeks time t fix it. Honestly I think he doesn't know how to use PDO at the first place but what to do..he's my so called SENIOR. – Kalaivani Nair Aug 14 '14 at 06:06
  • @veronicageorge What about `mysqli` ? It's just adding "i" in your `mysql_` functions (Or well, not just that, but you get my point). Not as hard / time consuming as switching from PDO. – Clément Malet Aug 14 '14 at 06:07

2 Answers2

0

Ignoring the fact that you're using a deprecated, inherently un-secure and unmaintained extension, you need to use the multiple attribute on your <select> element and let PHP know that it will be receiving an array of values by using the [] suffix on the element name. To summarise...

<select name="addon[]" multiple>
    <?php foreach($collection as $val => $label) : ?>
    <option value="<?= htmlspecialchars($val) ?>"><?= htmlspecialchars($label) ?></option>
    <?php endforeach ?>
</select>

The PHP variable $_POST['addon'] will then contain an array of selected values (when the form is posted of course).

Phil
  • 157,677
  • 23
  • 242
  • 245
0

You could use SESSION' variables to store all the ID :

session_start(); 

// Rest of your code here

// $addon_id=$_POST['addon'];  Becomes : 
if (!in_array($_POST['addon'], $_SESSION['addons'])) 
   $_SESSION['addons'][] = $_POST['addon'];

Edit: Not sure about your edit with sessions. You're resetting $_SESSION['option'] everytime, losing previous addon_id values

Clément Malet
  • 5,062
  • 3
  • 29
  • 48