简体   繁体   中英

Echo new lines and unescaped values in textarea during Post Redirect Get routine

Trying to keep new lines and unescaped values intact in a textarea being repopulated with data during a PRG cycle. At what point do you assign the variable correctly so that new lines are recognized?

I've tried double quotes, nl2br , htmlentities , stripslashes but I can't seem to get it. Some attempts:

Assigning during the intial prg $_SESSION array:

$_SESSION['prg']['textarea'] = "$textarea";

When passing from prg array to var:

$textarea = htmlentities($_SESSION['prg']['textarea']); 

When echoing into the textarea:

<textarea name="textarea"><?php if(isset($textarea)) echo nl2br($textarea); ?></textarea>

And various combinations of the above, including the initial $_POST , directly after sanitizing.

Also, in case anyone asks: the escaping works as intended, db insert results are fine. It's just the form repopulating that's throwing things off.

I'm sure this is just a symptom of amateur hour... Looking for php/html solution only. Thanks in advance.

I don't think you want to be calling nl2br when you populate the text area if you want to keep the newlines showing up properly in the textarea. The htmlentities part is good though.

在存储数据时,使用addslashes($_POST['textarea']) ,在显示数据时使用stripslashes($textarea)

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