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
https://forums.phpfreaks.com/topic/194221-dropdown-list-help/
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
https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1021903
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
https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022285
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
https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022300
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
https://forums.phpfreaks.com/topic/194221-dropdown-list-help/#findComment-1022304
Share on other sites

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.