Jump to content

radio button and dropdown


cherylinn

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.