6

I have a select dropdown in html but it won't select multiple values. here is the code that I have for this:

<div class="col-sm-10">          
    <select multiple id="cmbService" name="cmbService" class="form-control" >
        <option value="0">- Select One -</option>
            <?php                                       
                try{
                    $dbHost = "localhost";
                    $dbUser = "mdchadmin";
                    $dbPass = "123456";
                    $dbName = "mdch_new";

                    $conn = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    } 
                    $sql = "SELECT PROMO_NUMBER, PROMO_NAME FROM PROMOS where status=1";
                    $result = $conn->query($sql);
                    if ($result->num_rows > 0) {
                        while($row = $result->fetch_assoc()) {
                            echo "<option value=\"{$row['PROMO_NUMBER']}\">{$row['PROMO_NAME']}</option>";
                        }
                    }                                       
                    $conn->close(); 
                }catch (Exception $e) {
                    echo 'Error: ' . $e->getMessage();
                }                                           
            ?>
    </select>
</div>

EDIT: IT NOW WORKS THANKS TO THE ANSWER. BUT NOW I HAVE A NEW PROBLEM(kinda) and i'm going thru this

so i did what u suggested, and im getting this $customer array in my other php file which results to:

te,GIAN MARCO.'_'.1235
g,g.'_'.123

where 1235 and 123 are  the  data on the mobile numbers column.
the problem is , when i do 

$mobile=(explode("_",$customers));
it doesn't give me anything when i output it via: 

foreach($mobile as $z) {
echo $z; echo "<br>";}

@identity unknown

Gian Marco Te
  • 112
  • 1
  • 2
  • 14
  • Maybe try stripping out the PHP to get the effect you want in HTML first, then make it dynamic? – pete23 Jun 13 '15 at 14:04
  • If I remove the php then the select will not retrieve the values from the database which the user will choose from.. I'm not sure I understand, what do you mean by making it dynamic? – Gian Marco Te Jun 13 '15 at 14:07
  • I mean: if you can get multiple select working for test data in HTML without the PHP, then you have identified whether your problem is in the PHP or the HTML. Debugging is all about narrowing down the problem space... – pete23 Jun 13 '15 at 14:09
  • Unfortunately, removing the php and adding test data by using tags then choosing multiple values don't work – Gian Marco Te Jun 13 '15 at 14:11
  • So is the problem being able to select multiple option elements on the HTML side, or receiving multiple options on the PHP side? – j08691 Jun 13 '15 at 14:13
  • Thanks to the proposed answer, I made it like this, – Gian Marco Te Jun 13 '15 at 14:16
  • so...is it working for you? – Abhinav Jun 13 '15 at 14:18
  • It's now selecting multiple values, I will now try to code it to send it to the other php file using post – Gian Marco Te Jun 13 '15 at 14:19

1 Answers1

7

You simply need to add an square bracket in your name attribute name="cmbService[]"

 <select id="cmbService" name="cmbService[]" class="form-control multiple " >
Abhinav
  • 8,028
  • 12
  • 48
  • 89
  • I made it like this: – Gian Marco Te Jun 13 '15 at 14:18
  • Do not forget to accept if this answer solved your problem :) – Abhinav Jun 13 '15 at 14:30
  • Okay it works now, but I have 1 more question. I can send the multiple values selected as an array to another php file by using post. I need now to use that array in a query.. How can I do this? I need to output the names of the services but the values that are being sent to the array is the id of each value in the database instead of their name.. – Gian Marco Te Jun 13 '15 at 14:44
  • In your value attribute of the "`; – Abhinav Jun 13 '15 at 14:47
  • btw i also used this on a customer dropdown and I still need to query for the selected values' mobile numbers :) because I'm using twilio to send sms to each selected customer so i need your help (again) to query the db for their #.. thank u for your help. – Gian Marco Te Jun 13 '15 at 15:03
  • You should include both the name and id combining using (underscore) **_** in the value attribute like echo ""; Then from the php script, explode that value using "unserscore" as the delimiter and then use the name to display and id to query the database. – Abhinav Jun 13 '15 at 15:07
  • so i did what u suggested, and im getting this $customer array in my other php file which results to: te,GIAN MARCO.'_'.1235 g,g.'_'.123 where 1235 and 123 are the data on the mobile numbers column. the problem is , when i do $mobile=(explode("_",$customers)); it doesn't give me anything when i output it via: foreach($mobile as $z) { echo $z; echo "
    ";}
    – Gian Marco Te Jun 13 '15 at 15:23
  • I think you should explode the $z in your foreach loop coz thats what the values are – Abhinav Jun 13 '15 at 15:27