Jump to content

Dropdown list help


max_power

Recommended Posts

Hi all,

 

Within in an editing form, how does one make sure that the option that was already selected in drop down list doesn't appear twice (as a selected option and as a normal option) So something like:

 

Edit form:

option 1 – selected option (say the value is a)

option 3 – b

option 4 – c

 

and not this:

 

option 1 – selected option (say the value is a)

option 2 – a

option 3 – b

option 4 – c

 

Thanks

Link to comment
Share on other sites

Yep, it is dynamically generated from a mySQL db.

 

Here is the code:

 

<tr> 
   <td width="150" class="label">Relationship</td>
   <td class="content"> <select name="relationship" id="relationship">
						<option selected="selected"><?php echo $relationship; ?></option>
						<?php
						$sqlQuery = "select relationship_id, name from tbl_relationship";
						$result = mysql_query($sqlQuery);
						while($row = mysql_fetch_assoc($result)) {
							$id = $row["relationship_id"];
							$name = $row["name"];
							echo "<option>$name</option>\n";					
						}
						?>
						</select>

   </td>

 

$relationship being the variable.

Link to comment
Share on other sites

I got it to work another way but I have a similar problem for a static dropdown list..

 

Would anyone know how to ensure the selected option doesnt appear twice for the hard coded option values, when editing the form?

 

The code is:

 

<select name="state" id="state" class="box">
<option selected="selected" ><?php echo $state; ?></option>
<option>ACT</option>
<option>NSW</option>
<option>Victoria</option>
<option>WA</option>
<option>SA</option>
<option>Tasmania</option>
<option>NT</option>
<option>Queensland</option>
</select>

Link to comment
Share on other sites

$states = array('ACT', 'NSW', 'Victoria');
// this is just to indicate don't mix your current $state value with the one in the foreach loop
$selected_state = $state; 

foreach($states as $state) {
   if($state == $selected_state) {
    echo '<option selected="selected">';
  } else {
    echo '<option>';
  }
  echo $state . '</option>';
}

Link to comment
Share on other sites

SINCE the first item is selected by default so the following statement makes the first option repeat twice

 

<option selected="selected" ><?php echo $state; ?></option>

 

//other options down in the list

 

so try removing the line

<option selected="selected" ><?php echo $state; ?></option>

 

& keep the other options

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.