HughbertD Posted December 11, 2007 Share Posted December 11, 2007 I have a <select> drop down, which is populated by data from a mysql table. I have created a form which allows me to click an Edit link, and will bring up all the table data in the relevant text boxes and allow me to update it. As I want to use a drop down box for one of the fields, is there anyway I can set the default of a the drop down to a specific value? This way the user will know which option is already in the field incase he/she doesn't want to edit that particular value Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted December 11, 2007 Share Posted December 11, 2007 yeah there is. this is partly an html question. <select name="dropdown"> <option id="1">option 1</option> <option id="2">option 2</option> <option id="3" SELECTED>option 3</option> <option id="4">option 4</option> <option id="5">option 5</option> </select> now if you want to dynamically pre-select the dropdown menu, you'll have to use some if/else statements in there. it's really easy, but implementing it will depend on where you're pulling these results from and whatnot. Quote Link to comment Share on other sites More sharing options...
atlanta Posted December 11, 2007 Share Posted December 11, 2007 Post up your code. Im guessing when you press edit you want that drop down box to select the category of the item you are selecting correct? Quote Link to comment Share on other sites More sharing options...
HughbertD Posted December 12, 2007 Author Share Posted December 12, 2007 Sorry for the lateness of this reply! Code I have is as follows: <?php //connect to MySQL $connect = mysql_connect("localhost","root","michael") or die ("Could not connect to database."); //choose the database mysql_select_db("jostark"); //get data from database $query = mysql_query("SELECT `companyID`,`compName`, `compCity`, `compCountry` FROM `placement` ORDER BY `companyID` ASC") or die (mysql_error()); echo "<select name='placement'>\n"; while ($data = mysql_fetch_array($query, MYSQL_ASSOC)) { echo "<option value='{$data['companyID']}'6>{$data['compName']}, {$data['compCity']}, {$data['compCountry']}</option>\n"; } echo "</select>\n"; This is how I am displaying my data from the table in the drop down select box. What I need now is to pre-select the drop down. I have a variable which holds the information. Any help would be really appreciated (especially by Tom (ha!)) Quote Link to comment Share on other sites More sharing options...
marcus Posted December 12, 2007 Share Posted December 12, 2007 <?php //connect to MySQL $connect = mysql_connect("localhost", "root", "michael") or die("Could not connect to database."); //choose the database mysql_select_db("jostark"); //get data from database $query = mysql_query("SELECT `companyID`,`compName`, `compCity`, `compCountry` FROM `placement` ORDER BY `companyID` ASC") or die(mysql_error()); echo "<select name='placement'>\n"; while ($data = mysql_fetch_array($query, MYSQL_ASSOC)) { $sql = "SELECT * FROM `table` WHERE `companyID`='".$data['companyID']."'"; $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res); $sel = ($row['companyID'] == $data['companyID']) ? " SELECTED" : ""; echo "<option value='".$data['companyID']."'6".$sel.">".$data['compName'].", ".$data['compCity'].", ".$data['compCountry']."</option>\n"; } echo "</select>\n"; ?> edit the $sql variable to fit your query needs Quote Link to comment 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.