Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dymanic SELECTED In Listbox

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
This dynamic listbox is working fine except for the SELECTED which is always for the first item no matter what is selected and it's likely to do with the fact that the select has an on submit function. The Onsubmit function is required so is there a workaround for the SELECTED?

Code:
<select name="FormName" onchange="this.form.submit()">
<?php
if ($result = $mysqli->query($LevelQuery1)) {
	$i = 1;
	while ($row = $result->fetch_row()) {
		if ($i = 0) {
			$value = "SELECTED value=\"" . $row[0] . "\"";
		} else {
			$value = "value=\"" . $row[0] . "\"";
		}
	$selectBox_g_a0 .= "<option " . $value . ">" . $row[0] . "</option>\n";
	$i++;
	}
    $result->close();
}
echo $selectBox_g_a0;
?>
</select>
 
Not sure I follow, your code will set the SELECTED parameter to all options because you are assignment of a value is always true. Setting $i to 0 will always be true. So all your dropdown options will have the SELECTED parameter.

The comparison operator is a double equal sign $i [red]==[/red] 0. However since $i starts at 1 that will never be true, so nothing will ever be SELECTED if you change that.

Regardless, unless you can check against something, there's no way to know which value should be selected. Checking against a changing value inside the loop that actually changes is futile to say the least.






----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
You're right, what was I thinking? This does what I need:

Code:
<select name="_selectname" onchange="this.form.submit()">
<?php
if ($result = $mysqli->query($LevelQuery1)) {
	$i = 0;
	while ($row = $result->fetch_row()) {
		if [COLOR=red]($_POST['_selectname'] == $row[0])[/color] {
			$value = "SELECTED value=\"" . $row[0] . "\"";
		} else {
			$value = "value=\"" . $row[0] . "\"";
		}
	 .= "<option " . $value . ">" . $row[0] . "</option>\n";
	$i++;
	}
    $result->close();
}
echo $selectBox_selectname;
?>
</select>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top