简体   繁体   中英

hidden attribute in input tag from html form

I am trying to get the posted information and display the info using the following code:

PHP code:

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

HTML code :

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

The code above will work as I intented. However, if I get rid of the input name="send" type="hidden", the user input message will not show up once the send button is clicked. Why would this happen?

You need to add name='send' to your submit button, your PHP code is reading the name of the form elements, and you have not specified one for your submit button.

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

Also, a quick note - you can change your form method to GET instead of POST to easily see what form data you're sending in the URL bar.

This is because you are checking that the POST variable "send" isset. That is what you named your hidden input.

You should add a name to your submit input. Example:

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

Now in your php, check for the name of your submit button. I used "submit_button" in this example. Here is modified code example:

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

Dont have to bother naming your send button or anything, just remove that hidden line...

and change your php to....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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