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
https://forums.phpfreaks.com/topic/264564-radio-button-and-dropdown/
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>

  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 

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.

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>

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> 

Archived

This topic is now archived and is closed to further replies.

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