Jump to content

<select> default - (maybe a little offtopic?)


HughbertD

Recommended Posts

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

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.

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!))

 

 

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.