Member_2_5230414
asked on
choosing the selected opetion with php in menu
why does my code not select the previously selected from the drop down menu?
$_SESSION['industry_id1'] holds the value of the field
$_SESSION['industry_id1'] holds the value of the field
<select name="type_of_work_id" id="type_of_work_id">
<option value="<?php echo $_SESSION['type_of_work_id1'] ;?>" selected="selected">- Please Select -</option>
<?php if (mysql_num_rows($rst)>0):
while ($rowt = mysql_fetch_assoc($rst)):?>
<option <?php if ($rowt['id'] == $_SESSION['type_of_work_id1']){ ?>selected="selected" <? } ?> value="<?=$rowt['id']?>"><?=stripslashes($rowt['title'])?></option>
<?php endwhile;
endif; ?>
</select>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
how would i get sallary to show as it just selects the bottom option each time
<li><label>Salary:</label>
<select name="salary_id" id="salary_id">
<option value="<?php echo $_SESSION['salary_id1'] ;?>" selected="selected">- Please Select -</option>
<?php if (mysql_num_rows($rss)>0):
while ($rows = mysql_fetch_assoc($rss)):
?>
<option value="<?=$rowi['id']?>" <? if ($rowi['id']==$_SESSION['salary_id1']) echo 'selected="selected"';?>>£<?=formatcurrency($rows['salary_from'])?> to £<?=formatcurrency($rows['salary_to'])?>
</option>
<?php endwhile;
endif; ?>
<option value="31">250,000 +</option>
</select><?php if ( $_SESSION['salary_id'] == '1') { echo '<font color="red">* required</font>';} ?>
</li>
Your WHILE loop is populating a variable called $rows and your code is using $rowi
Change one of them to match the other and try again
Change one of them to match the other and try again
$select = "<select name='name' id='id'><option value=''>- Please Select -</option>";
while ($rowt = mysql_fetch_assoc($rst)) {
// get the id from the result
$id = $rowt['id'];
$type_of_work = $rowt['type-of-work']; // or whatever it is
// check if it matches the SESSION value
if ($id == $_SESSION['type_of_work_id
$select .= "<option selected value=".$id.">".$type_of_w
} else {
$select .= "<option value=".$id.">".$type_of_w
}
}
$select .= "</select>";
Now the right choice will be selected. You can put this anywhere in the page you like. If it is going among the HTML you would do this..
<html>
<head></head>
<body>
<?php echo $select; ?>
</body>
</html>
Done and easy to. Hope this helps!