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 Link to comment https://forums.phpfreaks.com/topic/81231-default-maybe-a-little-offtopic/ 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. Link to comment https://forums.phpfreaks.com/topic/81231-default-maybe-a-little-offtopic/#findComment-412249 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? Link to comment https://forums.phpfreaks.com/topic/81231-default-maybe-a-little-offtopic/#findComment-412275 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!)) Link to comment https://forums.phpfreaks.com/topic/81231-default-maybe-a-little-offtopic/#findComment-413110 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 Link to comment https://forums.phpfreaks.com/topic/81231-default-maybe-a-little-offtopic/#findComment-413118 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.