简体   繁体   English

下拉数据未发送到 $_POST php

[英]Dropdown data not being sent to $_POST php

I'm new to coding, and have encountered some difficulty with the drop-down list.我是编码新手,在使用下拉列表时遇到了一些困难。 Would appreciate any help given!将不胜感激任何帮助!

I have this:我有这个:

<html>
    <select name="Subject">
    <option value="One">One</option>
    <option value="Two">Two</option>
    </select>
</html>

<?php
if (isset($_POST['submit'])) {
    echo $_POST['Subject']; 
}
echo '
<form method="post"><input type="submit" name="submit" value="Submit Option!"></form>';
?>

This returns me with an unidentified index error for 'Subject' whenever I hit the Submit Option button.每当我点击提交选项按钮时,这都会为我返回一个未识别的“主题”索引错误。 I did a print_r($_POST) and realized that my selected options for the drop-down list "Subject" did not pass through.我做了一个print_r($_POST)并意识到我为下拉列表“主题”选择的选项没有通过。 (ie The $_POST array that was printed did not show anything selected options from the drop-down list) (即打印的$_POST数组没有显示任何从下拉列表中选择的选项)

One of the first things to know about HTML forms is that, when a form is submitted, the information contained within it gets submitted.其中的第一件事情了解HTML表单的是,当一个表单提交,包含在它被提交的信息。 To submit a value for Subject, that field needs to be contained within the <form> element.要提交主题的值,该字段需要包含在<form>元素中。

<html>
    <?php
    if (isset($_POST['submit'])) {
        echo $_POST['Subject']; 
    }
    ?>
    <form method="post">
        <select name="Subject">
            <option value="One">One</option>
            <option value="Two">Two</option>
        </select>

        <input type="submit" name="submit" value="Submit Option!">
    </form>
</html>

Put <form> code above the <select> like below:-<form>代码放在<select>上方,如下所示:-

<html>
    <form method="post">

    <select name="Subject">
    <option value="One">One</option>
    <option value="Two">Two</option>
    </select>

    <input type="submit" name="submit" value="Submit Option!"></form>
</html>
<?php
if (isset($_POST['submit'])){
echo $_POST['Subject']; 
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM