Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

Need to set variable = to dropdown selection in php/html

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...