Jump to content

show selected on form via session == true


runnerjp

Recommended Posts

if the session is true $_SESSION['frommonth'] i want it to show the selected session

 

<select name="from_month" id="from_month">
						<option value="<?php echo $_SESSION['frommonth'];?>" selected="selected">mm</dd>
						<?for ($i=1;$i<=12;$i++):?>
						<option value="<?=$i?>"><?=$months[$i]?></dd>
						<?endfor;?>
					</select>

 

how could i do this?

 

 

Link to comment
https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/
Share on other sites

In this case, I prefer to assign a variable and do the check before the HTML:

 

session_start()
if(isset($_SESSION['frommonth'])){
$fmonth = $_SESSION['frommonth'];
}else{
$fmonth = "None";
}
<select name="from_month" id="from_month">
						<option value="<?php echo $fmonth;?>" selected="selected">mm</dd>
						<?for ($i=1;$i<=12;$i++):?>
						<option value="<?=$i?>"><?=$months[$i]?></dd>
						<?endfor;?>
					</select>

Not tested.

i gave it ago and it didnt select the month

 

<select name="from_month" id="from_month">

                     

<option value="" selected="selected">mm</dd>

<?php for($i = 1; $i <= 12; $i++){

    $sel = ($i == $_SESSION['frommonth']) ? " SELECTED" : "";

    echo '<option value="'.$i.'"'.$sel.'>'.$months[$i].'</option>';

}?>

</select>

yes that worked.... last question

 

how would i do it for a year?

<select name="from_year" id="from_year">

<?php for($i=0;$i<=100;$i++){

    $sel = ($i == $_SESSION['fromyear']) ? " SELECTED" : "";

    echo '<option value="'.$i.'"'.$sel.'>'.$year[$i].'</option>';

}?>

 

<option value="<?php echo $_SESSION['fromyear'];?>" selected="selected">yyyy</dd>

<?for ($i=0;$i<=100;$i++):?>

<option value="<?=$start_year-$i?>"><?=$start_year-$i?></dd>

<?endfor;?>

</select>

 

 

i hav tried

<?php for($i=0;$i<=100;$i++){
     $sel = ($i == $_SESSION['fromyear']) ? " SELECTED" : "";
     echo '<option value="'.$i.'"'.$sel.'>'.$year[$i].'</option>';
}?>

actually to add to that how would i do it also for code such as

 

<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 value="<?=$rowt['id']?>"><?=stripslashes($rowt['title'])?></option>
						   <?php endwhile;
						endif; ?>
				  </select>

i have tried the following suggestion but the field is blank for some reason

 

<select name="from_year" id="from_year">

<?php for($i = 1900; $i <= 2012; $i++){
     $sel = ($i == $_SESSION['fromyear']) ? " SELECTED" : "";
     echo '<option value="'.$i.'"'.$sel.'>'.$year[$i].'</option>';
}?>
					</select>

ah yes i have no idea why i added year as its a number.

 

so when faced with my selected boxes getting results from the database

<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 value="<?=$rowt['id']?>"><?=stripslashes($rowt['title'])?></option>
						   <?php endwhile;
						endif; ?>
				  </select>

 

how would i use the selected statement?

 

this is my last one i promis as then i would have them all :)

Well for one... get rid of the selected in the first option value that says "Please select..."

 

while($rowt = mysql_fetch_assoc($rst)){
    $sel = ($_SESSION['type_of_work_id1'] == $rowt['type_of_work_id']) ? " SELECTED" : ""; // $rowt['type_of_work_id'] is what i'm assuming, change if not
    // rest of code
}

the following give me an error

 

i need to keep <option value="<?php echo $_SESSION['type_of_work_id1']  ;?>" selected="selected">- Please Select -</option> other wise it will chose an option.

 

<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)){
    $sel = ($_SESSION['type_of_work_id1'] == $rowt['type_of_work_id']) ? " SELECTED" : "";?>
						   <option <?php if ($rowt['id'] == $_SESSION['type_of_work_id1']){ ?>selected="selected" <? } ?> value="<?=$rowt['id']?>"><?=stripslashes($rowt['title'])?></option>
						   <?php }endwhile;
						endif; ?>
				  </select>

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.