0

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!!

2
  • The action is not in the submit button but in the form's opening tag. Where's the form opening tag? Commented Aug 14, 2012 at 5:59
  • By the way, you can shorten the echos to the form of <?=$value0?> Commented Aug 14, 2012 at 6:01

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.