runnerjp Posted April 18, 2012 Share Posted April 18, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/ Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338489 Share on other sites More sharing options...
marcus Posted April 18, 2012 Share Posted April 18, 2012 for($i = 1; $i <= 12; $i++){ $sel = ($i == $_SESSION['frommonth']) ? " SELECTED" : ""; echo '<option value="'.$i.'"'.$sel.'>'.$months[$i].'</option>'; } Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338490 Share on other sites More sharing options...
batwimp Posted April 18, 2012 Share Posted April 18, 2012 Sorry, I misunderstood the question. Use mgallforever's code. Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338492 Share on other sites More sharing options...
runnerjp Posted April 18, 2012 Author Share Posted April 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338494 Share on other sites More sharing options...
marcus Posted April 18, 2012 Share Posted April 18, 2012 You can't have multiple options selected at once... Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338497 Share on other sites More sharing options...
runnerjp Posted April 18, 2012 Author Share Posted April 18, 2012 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>'; }?> Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338499 Share on other sites More sharing options...
marcus Posted April 18, 2012 Share Posted April 18, 2012 Well I'm assuming your session is storing it as a four digit number and not a max of 3 digits. for($i = 1900; $i <= 2012; $i++){ // code } Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338502 Share on other sites More sharing options...
runnerjp Posted April 18, 2012 Author Share Posted April 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338503 Share on other sites More sharing options...
runnerjp Posted April 18, 2012 Author Share Posted April 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338504 Share on other sites More sharing options...
marcus Posted April 18, 2012 Share Posted April 18, 2012 Why would you use $year[i$] the year is just $i Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338506 Share on other sites More sharing options...
runnerjp Posted April 18, 2012 Author Share Posted April 18, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338511 Share on other sites More sharing options...
marcus Posted April 18, 2012 Share Posted April 18, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338515 Share on other sites More sharing options...
runnerjp Posted April 18, 2012 Author Share Posted April 18, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338545 Share on other sites More sharing options...
marcus Posted April 18, 2012 Share Posted April 18, 2012 you would do (inside your while loop) $sel = ($_SESSION['type_of_work_id1'] == $rowt['id']) ? " SELECTED" : ""; echo "<option value=\"".$rowt['id']."\"".$sel.">".stripslashes($rowt['title'])."</option>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/261181-show-selected-on-form-via-session-true/#findComment-1338570 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.