首页 > 解决方案 > 即使我添加了名称,HTML 表单也不起作用

问题描述

我正在用 HTML 制作表单,但遇到了这个错误:

                <form action="/Profile/new/poll/posted" method="post">
                <input class="hover-edit h2 hor-center" id="title" title="Poll's title ('New Poll' is not allowed)" value="New Poll" style="position: relative;text-align: center;width:95%;margin-top:2%;">
                <hr>
                <p><label for="opt1">Option 1</label> <input type="radio" disabled class="option"><input type="text" class="hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt1" id="opt1" value="Option 1"> &nbsp; <button type="button">Disable</button></button></p>
                <p><label for="opt2">Option 2</label> <input type="radio" disabled class="option"><input type="text" class="hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt2" id="opt2" value="Option 2"> &nbsp; <button type="button">Disable</button></button></p>
                <p><label for="opt3">Option 3</label> <input type="radio" disabled class="option"><input type="text" class="hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt3" id="opt3" value="Option 3"> &nbsp; <button type="button">Disable</button></button></p>
                <p><label for="opt4">Option 4</label> <input type="radio" disabled class="option"><input type="text" class="hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt4" id="opt4" value="Option 4"> &nbsp; <button type="button">Disable</button></button></p>
                <p><label for="opt5">Option 5</label> <input type="radio" disabled class="option"><input type="text" class="hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt5" id="opt5" value="Option 5"> &nbsp; <button type="button">Disable</button></button></p>
                <button type="submit" class="signupbtn" style="margin:2%;width:96%;">Post</button>
            </form>

上面的代码有一个表单、一些标签和一些带有名称的文本输入。但是,在操作页面上,它们都没有被定义。我尝试查看 $_POST 但仍然没有。更具体地说,我使用了:

    $opt1 = $_POST["opt1"];
    $opt2 = $_POST["opt2"];
    $opt3 = $_POST["opt3"];
    $opt4 = $_POST["opt4"];
    $opt5 = $_POST["opt5"];
    echo $opt1;
    echo $opt2;
    echo $opt3;
    echo $opt4;
    echo $opt5;

但是 PHP 对我代码中的每一行都返回了警告。

标签: phphtmlforms

解决方案


表单中禁用的输入元素永远不会被提交。正如@delboy1978uk 指出的那样https://www.w3schools.com/tags/att_input_disabled.asp

所以而不是

<p><label for="opt1">Option 1</label> <input type="radio" disabled class="option"><input type="text" class="hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt1" id="opt1" value="Option 1"> &nbsp; <button type="button">Disable</button></button></p>

<p><label for="opt1">Option 1</label> <input type="radio" class="option hover-edit" style="margin-top:1%;margin-bottom:1%;" name="opt1" id="opt1" value="Option 1"/></p>

顺便说一句,我不确定我是否理解您的代码,您有标签,然后是输入(收音机),然后是输入(文本),然后是按钮,然后是关闭按钮标签。

如果你用输入打开一个标签,你用输入关闭<input type="button"></input>,如果你用按钮关闭按钮<button></button>,不要混合这两个标签,这是不一样的。


推荐阅读