Link to home
Start Free TrialLog in
Avatar of Member_2_5230414
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

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

Open in new window

Avatar of Mark Brady
Mark Brady
Flag of United States of America image

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

<html>
<head></head>
<body>
<?php echo $select; ?>
</body>
</html>

Done and easy to. Hope this helps!
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_5230414
Member_2_5230414

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"';?>>&pound;<?=formatcurrency($rows['salary_from'])?> to &pound;<?=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>

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