Jump to content

Where to store Comments (Preview)?


doubledee

Recommended Posts

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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>';
	}
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.