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

Link to comment
Share on other sites

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
Share on other sites

<?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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.