choosing the selected opetion with php in menu

Member_2_5230414 used Ask the Experts™
why does my code not select the previously selected from the drop down menu?
$_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; ?>

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mark BradyPrincipal Data Engineer

Because your code is horrible and very hard to follow. Why don't you set it out like this.

$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_id1']) {
        $select .= "<option selected value=".$id.">".$type_of_work."</option>";
    } else {
        $select .= "<option value=".$id.">".$type_of_work."</option>";

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

<?php echo $select; ?>

Done and easy to. Hope this helps!
This is working OK for me

<select name="type_of_work_id" id="typeof_work_id">
     <option value="-1">- Please Select -</option>
          while ($rowt = mysql_fetch_assoc($rst)) {
               $selected = ($rowt['id'] == $_SESSION['type_of_work_id1']) ? " selected='selected' ": '';
               echo "<option value='{$rowt['id']}'$selected>". stripslashes($rowt['title']). "</option>\n";

Open in new window

Also in some browsers, underscores in the SELECT's name attribute can cause problems so rather than <select name="type_of_work_id" I would use <select name="typeOfWorkId"


how would i get sallary to show as it just selects the bottom option each time

						<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"';?>>&pound;<?=formatcurrency($rows['salary_from'])?> to &pound;<?=formatcurrency($rows['salary_to'])?>
							   <?php endwhile;
							endif; ?>
                            <option value="31">250,000 +</option>
					   </select><?php if ( $_SESSION['salary_id']   == '1') { echo '<font color="red">* required</font>';} ?>

Open in new window

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial