Page 1 of 1

Create a Dropdown menu in Php

Posted: Thu Apr 11, 2013 11:29 pm
by liveproject101
I want to create a drop down menu in php for my local host and tried following, But using it The drop down menu appears, but is blank, and has no options.

<?php
session_start();
//if the session data has been set, then the variable $sv_02 is defined
//as the data held in the session under that name, otherwise it is blank
if (isset($_SESSION['sv_02'])) {$sv_02=$_SESSION['sv_02'];} else {$sv_02="";}
//define the array
$dm_sv_02 = array('-Year','-2012','-2011','-2010','-2009');
//create the function
function dropdown($dropdownoptions, $session_data)
{
foreach($dropdownoptions as $dropdownoption){
if($session_data == $dropdownoption){
echo '<option value="' . $dropdownoption . '" selected>' . $dropdownoption . '</option>';
} else {
echo '<option value="' . $dropdownoption . '">' . $dropdownoption . '</option>';
}
}
}
//echo the HTML needed to create a drop down, and populate it with
//the function which should create the <option> elements
echo '<select name="sv_02">';
dropdown($dm_sv_02, $sv_02);
echo '</select>';
?>

Re: Create a Dropdown menu in Php

Posted: Fri Apr 12, 2013 12:57 am
by social_experiment
Are there any error, notice or warning messages displayed when you run the script? the script itself works, displays the dropdown menu with 'Year' and then the year values.

try using the php code button; it makes the code easier to read :)