I need some help please. I need to create a drop down list and then when the option is submitted a php script must run. The complication is I can't hard code any of the values. I need it to be 'dynamically created'
config.php
$number_Of_Option="3"
$Option_One_Name="Cars"
$Option_One_Year="2000"
$Option_One_Colour="Blue"
$Option_Two_Name="Houses"
$Option_Two_Year="2003"
$Option_Two_Colour="Pink"
$Option_Three_Name="Bikes"
$Option_Three_Year="1990"
$Option_Three_Colour="Orange"
Now I need the drop down to be made with the name in the drop down to be "Cars", "Houses", "Bikes" but they must be variable based so if I change "Cars" it will change.
Then w开发者_运维问答hen the option is submitted (Option Two in this case) I need $Option_Two_Year and $Option_Two_Colour to be passed to a script where these two variables are the variables in the script. So the script is always the same however if drop down one is selected then it uses variables 1 if 2 then it uses variables 2 etc. I also need to it work for an infinite number of options so at any time I can got into the config.php and add another option with its own variables. It can use arrays and jquery if that's easier or necessary. Thanks
Make an external file, eg. options.php, with this code:
$options = array();
$options["cars"] = array("2000","blue");
$options["houses"] = array("2003","pink");
$options["bikes"] = array("1990","orange");
Include this file in both the form page, and the parse script, using include("options.php");
In the form page, do:
include("options.php");
echo '<select name="option">';
foreach($options as $name => $values)
{ echo '<option value="' .$name .'">' .$name .'</option>';
}
echo '</select>';
In the parse script, do:
include("options.php");
$chosenValue = $_POST['option'];
list($year,$colour) = $options[$chosenValue];
Now $year
and $colour
will contain the year and colour for the option with the chosen name.
You know they invented arrays for this? If you got a reasonably recent version of PHP (I think 1.0 or higher ;) ), you could store this data like:
$options = array(
'Cars' => array('Year'=>'2000', 'Colour'=>'Blue'),
'Houses' => array('Year'=>'2003', 'Colour'=>'Pink'),
'Bikes' => array('Year'=>'1990', 'Colour'=>'Orange'));
Then you could get the selected option, and find the matching values by that option:
$option = $_GET['option'];
if (array_key_exists($option, $options))
{
$selectedYear = $options[$option]['Year'];
$selectedColour = $options[$option]['Colour'];
// Process them.
}
If you don't want to (or are not allowed to) use arrays, you'll need to match every variable:
if ($option === $Option_One_Name)
{
$selectedYear = $Option_One_Year;
$selectedColour = $Option_One_Colour;
}
elseif ($option === $Option_Two_Name)
{
...
Of course, you can speed this up a little bit. You can make an array of numbers and get the matching variables by their names
$numbers = array('One', 'Two', 'Three');
foreach($numbers as $number)
{
$optionVar = 'Option_' . $number . '_Name';
if ($option === $$optionVar)
{
$yearVar = 'Option_' . $number . '_Year';
$selectedYear = $$yearVar;
...
}
}
That code is more complex, but it won't need to be expanded if you get more option. You only need to update the Numbers array. But still, as you can see, it will be easier to use arrays.
精彩评论