doubledee Posted March 18, 2012 Share Posted March 18, 2012 I added the ability for Users to "preview" their Comments before submitting them. I just realized that when they click "Preview" that their Comments appear above the Comments form box - similar to how PHPFreaks works - but that the Comments form box loses it's data since the page reload causes this to lose things... <li> <label for="comments">Comments:</label> <textarea id="comments" name="comments" cols="50" rows="15"><?php if (isset($comments)){echo nl2br(htmlentities($comments, ENT_QUOTES));} ?></textarea> <?php if (!empty($errors['comments'])){ echo '<span class="error">' . $errors['comments'] . '</span>'; } ?> </li> What is the easiest way to maintain form persistence? (I really would like to avoid having to store things in my database during the preview.) How does PHPFreaks do things? I was going to use a SESSION, but since this is a Comments field that can take up to 65,535 characters, that probably won't work?! Any ideas? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/ Share on other sites More sharing options...
trq Posted March 18, 2012 Share Posted March 18, 2012 If you already have the data there ready to display above the form box, just place it in the form box as well. Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328707 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 If you already have the data there ready to display above the form box, just place it in the form box as well. It is super late and my brain is fading... Here is a *snippet* of my code... <?php // HANDLE FORM. if ($_SERVER['REQUEST_METHOD']=='POST'){ if (empty($errors)){ // Valid form data. // Check Submittal Type. if (isset($_POST['preview'])){ // Preview Comments. $preview = $_POST['comments']; }elseif (isset($_POST['submit'])){ // Submit Comments. // Set Comment Variables. $articleID = $_SESSION['articleID']; $memberID = $_SESSION['memberID']; $comments = $trimmed['comments']; ?> <!-- ADD A COMMENT --> <div id="boxMainContent"> <!-- PREVIEW COMMENT --> <?php if (!empty($preview)){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . nl2br(htmlentities($preview, ENT_QUOTES)) . '</p> </div>'; } ?> <!-- COMMENT FORM --> <div id="boxAddComment"> <form id="formAddComment" action="" method="post"> <!-- Comment --> <li> <label for="comments">Comments:</label> <textarea id="comments" name="comments" cols="50" rows="15"><?php if (isset($comments)){echo nl2br(htmlentities($comments, ENT_QUOTES));} ?></textarea> <?php if (!empty($errors['comments'])){ echo '<span class="error">' . $errors['comments'] . '</span>'; } ?> </li> How do I get the data from the Text Area to the variable $preview back to the Text Area? (Sorry, but it is like past 3:00am and I'm exhausted!) Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328708 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 Thorpe, I think this worked... // ************************** // Check for Form Errors. * // ************************** if (empty($errors)){ // Valid form data. // ************************ // Check Submittal Type. * // ************************ if (isset($_POST['preview'])){ // ******************** // Preview Comments. * // ******************** $preview = $trimmed['comments']; $comments = $trimmed['comments']; // $preview = $_POST['comments']; }elseif (isset($_POST['submit'])){ // ******************** // Submit Comments. * // ******************** // Set Comment Variables. $articleID = $_SESSION['articleID']; $memberID = $_SESSION['memberID']; $comments = $trimmed['comments']; $status = 'pending'; Now the only problem is that if I enter something like this... This is some really long comment that goes on and on and on.<br /> Line 2 Line 3 Line 4 Then when I hit "Preview", I get this in the Comments Text Area... This is some really long comment that goes on and on and on.<br /> <br /> Line 2<br /> <br /> Line 3<br /> <br /> Line 4 What is causing that?! Down below in my HTML section I have these two lines of code... <!-- PREVIEW COMMENT --> <?php if (!empty($preview)){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . nl2br(htmlentities($preview, ENT_QUOTES)) . '</p> </div>'; } ?> <!-- Comment --> <li> <label for="comments">Comments:</label> <textarea id="comments" name="comments" cols="50" rows="15"><?php if (isset($comments)){echo nl2br(htmlentities($comments, ENT_QUOTES));} ?></textarea> <?php if (!empty($errors['comments'])){ echo '<span class="error">' . $errors['comments'] . '</span>'; } ?> </li> Like I thought I said before, it seems to me that you are supposed to NOT use nl2br with some Form Type, and I'm thinking that it is Text Area?! Thanks, Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328709 Share on other sites More sharing options...
SaCH Posted March 18, 2012 Share Posted March 18, 2012 remove the htmlentities() function while you previewing the comment. then code will be something like this.. <!-- PREVIEW COMMENT --> <?php if (!empty($preview)){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . nl2br($preview, ENT_QUOTES) . '</p> </div>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328710 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 remove the htmlentities() function while you previewing the comment. then code will be something like this.. <!-- PREVIEW COMMENT --> <?php if (!empty($preview)){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . nl2br($preview, ENT_QUOTES) . '</p> </div>'; } ?> What is the logic behind that? I thought you *always* used HTMLEntities when you are *outputting* User's data/input?? And regardless, what is causing the <br />'s to appear?? Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328712 Share on other sites More sharing options...
SaCH Posted March 18, 2012 Share Posted March 18, 2012 remove the htmlentities() function while you previewing the comment. then code will be something like this.. <!-- PREVIEW COMMENT --> <?php if (!empty($preview)){ echo '<div id="boxPreview"> <h3>Preview</h3> <p>' . nl2br($preview, ENT_QUOTES) . '</p> </div>'; } ?> What is the logic behind that? I thought you *always* used HTMLEntities when you are *outputting* User's data/input?? And regardless, what is causing the <br />'s to appear?? Debbie Let me know the problem is solved with my code ? Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328715 Share on other sites More sharing options...
kicken Posted March 18, 2012 Share Posted March 18, 2012 remove the htmlentities() function while you previewing the comment. No, do not do that. You want to use both nl2br and htmlentities for your preview area. htmlentities to prevent XSS and nl2br to insert a <br> tag and the end of each line so they are visible in the html. The nl2br should not be used when you output them to the <textarea> though, as the newlines will be properly rendered there without needing the br tags. Continue using htmlentities however even in the textarea output. Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328728 Share on other sites More sharing options...
SaCH Posted March 18, 2012 Share Posted March 18, 2012 remove the htmlentities() function while you previewing the comment. No, do not do that. You want to use both nl2br and htmlentities for your preview area. htmlentities to prevent XSS and nl2br to insert a <br> tag and the end of each line so they are visible in the html. The nl2br should not be used when you output them to the <textarea> though, as the newlines will be properly rendered there without needing the br tags. Continue using htmlentities however even in the textarea output. if we use htmlentities() function while outputting the string & i think it won't work the break line (<br/>) it will just out put the <br/> tag with the string. ? Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328794 Share on other sites More sharing options...
doubledee Posted March 18, 2012 Author Share Posted March 18, 2012 remove the htmlentities() function while you previewing the comment. No, do not do that. You want to use both nl2br and htmlentities for your preview area. htmlentities to prevent XSS and nl2br to insert a <br> tag and the end of each line so they are visible in the html. The nl2br should not be used when you output them to the <textarea> though, as the newlines will be properly rendered there without needing the br tags. Continue using htmlentities however even in the textarea output. That was exactly what I was looking for!! (I knew it went something like that?!) Thanks Thorpe and Kicken!!! Debbie Quote Link to comment https://forums.phpfreaks.com/topic/259193-where-to-store-comments-preview/#findComment-1328813 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.