html表单中输入标记中的隐藏属性


hidden attribute in input tag from html form

我正在尝试获取发布的信息,并使用以下代码显示信息:

PHP代码:

        $self = $_SERVER['PHP_SELF'];
        if(isset($_POST['send'])){                
            $words = htmlspecialchars($_POST['board']);
            print "<b>".$words."</b>";
        }            ​​​​

HTML代码:

<form action="<?php $self ?>" method=post> <!--$self is the directory of the page itself-->
        <p><i>Comment</i></p>
        <textarea name="board" rows="20" cols="10"></textarea>
        <input name="send" type="hidden" />
        <p><input type='submit' value='send' /></p>
</form>  

上面的代码将按照我的意愿工作。但是,如果我去掉输入name="send"type="hidden",那么一旦单击发送按钮,用户输入消息就不会显示。为什么会发生这种情况?

您需要将name="send"添加到提交按钮,您的PHP代码正在读取表单元素的名称,并且您尚未为提交按钮指定名称。

<form action="<?php $self ?>" method=post> <!--$self is the directory of the page itself-->
        <p><i>Comment</i></p>
        <textarea name="board" rows="20" cols="10"></textarea>
        <p><input type='submit' name='send' value='send' /></p>
</form>  

另外,请注意,您可以将表单方法更改为GET,而不是POST,以便轻松查看您在URL栏中发送的表单数据。

这是因为您正在检查POST变量"send"是否已设置。这就是您对隐藏输入的命名。

您应该在提交输入中添加一个name。示例:

    <p><input type='submit' name="submit_button" value='send' /></p>

现在在php中,检查提交按钮的name。我在这个例子中使用了"submit_button"。以下是修改后的代码示例:

    $self = $_SERVER['PHP_SELF'];
    if(isset($_POST['submit_button'])){                
        $words = htmlspecialchars($_POST['board']);
        print "<b>".$words."</b>";
    }  

不必麻烦命名发送按钮或任何东西,只需删除hidden行。。。

并将您的php更改为…

 $self = $_SERVER['PHP_SELF'];
    if(isset($_POST)){                
        $words = htmlspecialchars($_POST['board']);
        print "<b>".$words."</b>";
    }