How to keep text inserted in a html <textarea> after a wrong submission?

后端 未结 4 558
温柔的废话
温柔的废话 2021-01-27 01:27

Suppose I have a form like this:

Section1:
Sec
相关标签:
4条回答
  • 2021-01-27 01:58

    Don't forget about htmlspecialchars(). This should help: https://developer.mozilla.org/en/HTML/Element/textarea

    <textarea name="post_text" cols="100" rows="20"><?php echo htmlspecialchars($_POST['post_text']);?></textarea>
    
    0 讨论(0)
  • 2021-01-27 01:58

    You could use the same approach, but put the echo between the opening and closing <textarea></textarea> tags, as the textarea doesn't have a 'value' (as such) it has textual content:

    <textarea name="post_text" cols="100" rows="20"><?php echo $_POST['textareaContent']; ?></textarea>
    
    0 讨论(0)
  • 2021-01-27 01:59

    You would put it inside the <textarea> element like so:

    <textarea name="post_text" cols="100" rows="20"><?php echo $_POST['post_text']; ?></textarea>
    

    However, calling the $_POST element directly is not best practice. You should rather do something like this:

    <textarea name="post_text" cols="100" rows="20">
    <?php echo $var = isset($_POST['post_text']) ? $_POST['post_text'] : ''; ?>
    </textarea>
    

    This stops an E_NOTICE error from being reported upon the first page-load.

    0 讨论(0)
  • 2021-01-27 02:00

    Use the $_POST variable like this.

    <textarea name="post_text" cols="100" rows="20"><?= isset($_POST['post_text'])?$_POST['post_text']:'' ?></textarea>
    

    the inline conditional checks if the $_POST['post_text'] is set to remove the NOTICE warning

    0 讨论(0)
提交回复
热议问题