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. Quote 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> Quote 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 Quote 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? Quote 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. Quote 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 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 Quote 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. Quote 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? Quote 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> Quote 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! Quote 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> Quote 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. Quote Link to comment https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/#findComment-1355892 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.