Jump to content

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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