stevochegeoj Posted January 29, 2021 Share Posted January 29, 2021 Quote I have this code here. i wanted to know how i can be able to make multiple selections from the returned data from the database and how to save them Quote <select name="troom" multiple class="form-control" required> <option selected="true" disabled="disabled">--Select a room--</option> $records =mysqli_query($con, "select * from room where TRoom not in (select TRoom from roombook where cin <= '$checkout' and cout >= '$checkin');"); while ($data = mysqli_fetch_array($records)) { echo "<option value='" . $data['TRoom'] . "'>" . $data['TRoom'] . "</option>"; //displaying data in option menu } } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/312075-multiple-selection-in-an-option-list/ Share on other sites More sharing options...
Barand Posted January 29, 2021 Share Posted January 29, 2021 Name the select "troom[]" so the options are posted as an array Then foreach ($_POST['troom'] as $room) { // save $room } Quote Link to comment https://forums.phpfreaks.com/topic/312075-multiple-selection-in-an-option-list/#findComment-1584124 Share on other sites More sharing options...
stevochegeoj Posted January 30, 2021 Author Share Posted January 30, 2021 i don't why its returning an empty list. or how will it display? i am learning php Quote Link to comment https://forums.phpfreaks.com/topic/312075-multiple-selection-in-an-option-list/#findComment-1584156 Share on other sites More sharing options...
Barand Posted January 30, 2021 Share Posted January 30, 2021 Example... <?php if ($_SERVER['REQUEST_METHOD']=='POST') { echo "<b>You selected</b><ul>"; foreach ($_POST['fruit'] as $fruit) { echo "<li>$fruit</li>"; // update database here } echo "</ul><hr>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> </head> <body> <h1>Example</h1> <hr> <form method="POST"> Fruit: <br> <select name="fruit[]" multiple size="10"> <option>Apple</option> <option>Banana</option> <option>Date</option> <option>Kiwi</option> <option>Lemon</option> <option>Melon</option> <option>Orange</option> <option>Pear</option> <option>Quince</option> </select> <br> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/312075-multiple-selection-in-an-option-list/#findComment-1584159 Share on other sites More sharing options...
stevochegeoj Posted January 30, 2021 Author Share Posted January 30, 2021 26 minutes ago, Barand said: Example... <?php if ($_SERVER['REQUEST_METHOD']=='POST') { echo "<b>You selected</b><ul>"; foreach ($_POST['fruit'] as $fruit) { echo "<li>$fruit</li>"; // update database here } echo "</ul><hr>"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> </head> <body> <h1>Example</h1> <hr> <form method="POST"> Fruit: <br> <select name="fruit[]" multiple size="10"> <option>Apple</option> <option>Banana</option> <option>Date</option> <option>Kiwi</option> <option>Lemon</option> <option>Melon</option> <option>Orange</option> <option>Pear</option> <option>Quince</option> </select> <br> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html> my problem with this is, the option list is populated from the database. Do you mind implementing the code you posted in the quoted code? <select name="troom" multiple class="form-control" required> <option selected="true" disabled="disabled">--Select a room--</option> $records =mysqli_query($con, "select * from room where TRoom not in (select TRoom from roombook where cin <= '$checkout' and cout >= '$checkin');"); while ($data = mysqli_fetch_array($records)) { echo "<option value='" . $data['TRoom'] . "'>" . $data['TRoom'] . "</option>"; //displaying data in option menu } } ?> </select> Quote Link to comment https://forums.phpfreaks.com/topic/312075-multiple-selection-in-an-option-list/#findComment-1584160 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.