drop down menu... pick selected option

Member_2_5230414
Member_2_5230414 used Ask the Experts™
on
<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>

Open in new window


if the session is true $_SESSION['frommonth'] i want it to show the selected session
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try something more like this
<select name="from_month" id="from_month">
                                          
                                          <?php for ($i=1;$i<=12;$i++) {
                                                           $options = "<option value='$i'";
                                                            if($_SESSION['frommonth'] == $i)
                                                                    $options .= 'selected ="selected"';
                                                             $options .=">$months[$i]</option>";
                                    
                                          }?>
                                    </select>

Author

Commented:
i gave the above ago but its makes the form blank
ha that is because I forgot to have you echo $options at the end
so
                                                    <?php for ($i=1;$i<=12;$i++) {
                                                           $options = "<option value='$i'";
                                                            if($_SESSION['frommonth'] == $i)
                                                                    $options .= 'selected ="selected"';
                                                             $options .=">$months[$i]</option>";
                                                       echo $options;
                                   
                                                   }?>

Author

Commented:
wicked so with that if i did year would i do
 <?php for ($i=0;$i<=100;$i++) {
                                                           $options = "<option value='$i'";
                                                            if($_SESSION['fromyear'] == $i)
                                                                    $options .= 'selected ="selected"';
                                                             $options .=">$start_year[$i]</option>";
                                                       echo $options;
                                   
                                                   }?>

Open in new window

as they where blank again

	<? $start_year = Date('Y')-0; ?>
						<select name="from_year" id="from_year">
							<option value="" selected="selected">yyyy</dd>
							<?for ($i=0;$i<=100;$i++):?>
							<option value="<?=$start_year-$i?>"><?=$start_year-$i?></dd>
							<?endfor;?>
						</select>

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
Can you please show us a link to a web site that does something like what you want?  If we can see the implementation it will be very easy to write the code!
Most Valuable Expert 2011
Top Expert 2016
Commented:
Maybe something like this?  I'm not quite sure what you want to achieve with the drop-down select list, but this is one of the general design patterns that seems to work.
www.laprbass.com/RAY_temp_runnerjp.php
<?php // RAY_temp_runnerjp.php
error_reporting(E_ALL);
echo "<pre>";

// REQUIRED FOR PHP 5.1+
date_default_timezone_set('America/Chicago');

// IF THERE IS ANY INPUT
if (!empty($_GET)) var_dump($_GET);

// CREATE A START YEAR VARIABLE
$start_year = Date('Y');

// HOW MANY YEARS OF HISTORICAL SELECT STATEMENTS DO WE WANT"
$n_years = 100;

// CREATE THE FORM
$html = '<form>';

// CREATE THE SELECT TAG AND THE DEFAULT VALUES
$html .= '<select name="from_year" id="from_year">' . PHP_EOL;
$html .= '<option value="" selected="selected">CHOOSE A YEAR</option>' . PHP_EOL;

// CREATE THE ADDITIONAL OPTIONS
for ($i=0; $i<=$n_years; $i++)
{
    $y = $start_year - $i;
    $html
    .= '<option value="'
    . $y
    . '">'
    . $y
    . '</option>'
    . PHP_EOL
    ;
}

// ADD THE SELECT END TAG
$html .= '</select>' . PHP_EOL;

// ADD A SUBMIT BUTTON
$html .= '<input type="submit" /></form>' . PHP_EOL;

echo $html;

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial