Hi All,
I am 'attempting' to start to use OOP for all the forms I build and here is my attempt at making a dynamic drop-down menu, that is reusable, and will check what the option selected was, for users that have 'not the best' browsers and lose their choices when they go back to fix a selection.
<?php
$FieldName = 'People';
$List = array('Don', 'Mike', 'Joe');
$selected1 = ((isset($_POST['People']) && !empty($_POST['People'])) ? in_array($grade, $_POST['People']) : false);
function create_dropdown($myarray, $selected_id)
{
$dropdown = "";
global $List;
foreach($List as $key=>$value)
{
$selected = "";
if($key == $selected_id)
{
$selected = "selected";
}
$dropdown .="<option $selected value=\"".$value."\"".$selected1.">".$value."</option>
";
}
return $dropdown;
}
$dropdown = '<select name="'.$FieldName.'" id="'.$FieldName.'" >';
$dropdown .= create_dropdown($myarray, 4);
$dropdown .= " </select>";
echo $dropdown;
?>
What I am attempting (and confused on) is I need to use this same code several times on the same form, but I want to define the FieldName and List outside the function, so I can just list that for each application in the same form.
But what I get is the same drop-down data list on the form.
IE
----Pick your school----
-----------A-----------
-----------B-----------
-----------C-----------
and for the next drop down, I get ...
--Are you happy--
---------A-------
---------B--------
---------C--------
instead of
--Are you happy--
-------Yes-------
-------No--------
Does that make sense?
Thanks in advance,
Don