Need to set variable = to dropdown selection in ph

2019-08-20 11:22发布

问题:

I am new to php and html, and I am required to make a drop down list along with other inputs to create parameters which will be used in a perl script.

I need the drop down to upon submission set a variable = to what the user selected.

My code so far:

<HTML>
<?php
$value0 = "";
$value1 = "Banana";
$value2 = "Apple";
$value3 = "Orange";
$submittedValue;
?>
<select project="FruitList" id="FruitList">
 <option value = <?php echo $value0; ?>><?php echo $value0; ?></option>
 <option value = <?php echo $value1; ?>><?php echo $value1; ?></option>
 <option value = <?php echo $value2; ?>><?php echo $value2; ?></option>
 <option value = <?php echo $value3; ?>><?php echo $value3; ?></option>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</HTML>

I am not sure how to use the submit button so that I can set the $submittedValue variable = to the selected value in the dropdown. HELP!!

回答1:

You can check submitted value with your existing options like below;

    <HTML>
        <?php
        $submittedValue = "";
        $value0 = "";
        $value1 = "Banana";
        $value2 = "Apple";
        $value3 = "Orange";
        if (isset($_POST["FruitList"])) {
            $submittedValue = $_POST["FruitList"];
        }
        ?>
        <form action="" name="fruits" method="post">
        <select project="FruitList" id="FruitList" name="FruitList">
         <option value = "<?php echo $value0; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value0; ?></option>
         <option value = "<?php echo $value1; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value1; ?></option>
         <option value = "<?php echo $value2; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value2; ?></option>
         <option value = "<?php echo $value3; ?>"<?php echo ($value0 == $submittedValue)?" SELECTED":""?>><?php echo $value3; ?></option>
        </select>
        <input type="submit" name="submit" id="submit" value="Submit" />
        </form>
        </HTML>

EDIT: I have corrected your html and add some functionality to check submitted value



回答2:

First of all you are missing the form action:

<HTML>
<?php
$value0 = "";
$value1 = "Banana";
$value2 = "Apple";
$value3 = "Orange";
$submittedValue;
?>
<form action"formProcessing.php" method="POST">

<select project="FruitList" id="FruitList">
<option value = <?php echo $value0; ?>><?php echo $value0; ?></option>
<option value = <?php echo $value1; ?>><?php echo $value1; ?></option>
<option value = <?php echo $value2; ?>><?php echo $value2; ?></option>
<option value = <?php echo $value3; ?>><?php echo $value3; ?></option>
<input type="submit" name="submit" id="submit" value="Submit" />
</form>
</HTML>

Then you need to create a new script formProcessing.php, you can access the form variables using $_POST variable.

<?php
function filter($data) {
$data = trim(htmlentities(strip_tags($data)));
if (get_magic_quotes_gpc())
$data = stripslashes($data);
$data = mysql_real_escape_string($data);
return $data;
}
foreach($_POST as $key => $value) {
 $mydata[$key] = filter($value);
}
echo $mydata['FruitList'];
?>

The filter method is used to escape the $_POST variable, in order to protect your Application from MySQL and XSS attacks. This should create a new copy of your $_POST variable however accessible through $mydata array.