cherylinn Posted June 21, 2012 Share Posted June 21, 2012 How do i get a radio button and dropdown list to remain selected? For example i select gender as female and apple in dropdown list and press submit button. When i press the back button to return, i want the radio button for female to stay selected and apple in dropdown list to remain selected. Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/ Share on other sites More sharing options...
Drummin Posted June 21, 2012 Share Posted June 21, 2012 Here's a few simple examples. <html> <body> <form method="post" action=""> <?php $genders=array("Male","Female"); foreach($genders as $gender){ $selected_gender=(isset($_POST['gender']) && $_POST['gender']==$gender ? 'checked="checked"':''); echo "<input type=\"radio\" name=\"gender\" value=\"$gender\" $selected_gender /> $gender "; } ?> <select name="fruit"> <?php $fruits=array("Apple","Bannana","Orange","Pear"); foreach($fruits as $fruit){ $selected_fruit=(isset($_POST['fruit']) && $_POST['fruit']==$fruit ? 'selected="selected"':''); echo "<option value=\"$fruit\" $selected_fruit>$fruit</option>"; } ?> </select> <input type="submit" value="submit" /> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355844 Share on other sites More sharing options...
xyph Posted June 21, 2012 Share Posted June 21, 2012 Pressing the back button is entirely client-side behaviour Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355848 Share on other sites More sharing options...
Drummin Posted June 21, 2012 Share Posted June 21, 2012 Right xyph, I didn't read the question well enough. Possibly save selected values to session? Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355853 Share on other sites More sharing options...
xyph Posted June 21, 2012 Share Posted June 21, 2012 You can try and no-cache a solution and hack together a session-based method, but again, this should be left to the client to decide. When I hit back, the old values are still there on my browser. Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355854 Share on other sites More sharing options...
cherylinn Posted June 21, 2012 Author Share Posted June 21, 2012 Quote Here's a few simple examples. <html> <body> <form method="post" action=""> <?php $genders=array("Male","Female"); foreach($genders as $gender){ $selected_gender=(isset($_POST['gender']) && $_POST['gender']==$gender ? 'checked="checked"':''); echo "<input type=\"radio\" name=\"gender\" value=\"$gender\" $selected_gender /> $gender "; } ?> <select name="fruit"> <?php $fruits=array("Apple","Bannana","Orange","Pear"); foreach($fruits as $fruit){ $selected_fruit=(isset($_POST['fruit']) && $_POST['fruit']==$fruit ? 'selected="selected"':''); echo "<option value=\"$fruit\" $selected_fruit>$fruit</option>"; } ?> </select> <input type="submit" value="submit" /> </form> </body> </html> for the dropdown list, we have to list everything out in the array in order to remain selected? Because my dropdown list consist of the date. For example, Day<select name=day value=''> <option value="">--Please Select--</option> <option value='01'>01</option> <option value='02'>02</option> <option value='03'>03</option> etc.. all the way to 31 Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355861 Share on other sites More sharing options...
Drummin Posted June 21, 2012 Share Posted June 21, 2012 You can use range for this. <select name="day"> <?php $days = date("t"); foreach(range(1,$days) as $day){ $selected_day=(isset($_POST['day']) && $_POST['day']==$day ? 'selected="selected"':''); echo "<option value=\"$day\" $selected_day>$day</option>"; } ?> </select> Note: I suppose using days in current month might me confusing or not what you need. You can use range(1,31) for the foreach statement. Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355866 Share on other sites More sharing options...
cherylinn Posted June 21, 2012 Author Share Posted June 21, 2012 Thanks, how about month where i need to create january , february etc? Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355878 Share on other sites More sharing options...
Drummin Posted June 21, 2012 Share Posted June 21, 2012 Array for months will work for this. <select name="month"> <?php $months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); foreach ($months as $month){ $selected_month=(isset($_POST['month']) && $_POST['month']==$month ? 'selected="selected"':''); echo "<option value=\"$month\" $selected_month>$month</option>"; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355881 Share on other sites More sharing options...
cherylinn Posted June 21, 2012 Author Share Posted June 21, 2012 okay! Thanks you! Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355882 Share on other sites More sharing options...
Drummin Posted June 21, 2012 Share Posted June 21, 2012 Let me guess, year is next? If you need it. <select name="year"> <?php $cyear = date("Y"); foreach(range(1930,$cyear) as $year){ $selected_year=(isset($_POST['year']) && $_POST['year']==$year ? 'selected="selected"':''); echo "<option value=\"$year\" $selected_year>$year</option>"; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355883 Share on other sites More sharing options...
cherylinn Posted June 21, 2012 Author Share Posted June 21, 2012 nope =) my assignment only require me to put dropbox for day and month and for year, a input box. I only left with validating the date. Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355892 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.