Jump to content

Help to prevent re-sending data in comment script


mlukac89

Recommended Posts

Hey all,

I have some problems with this script when i post a comment and try to refresh page in Firefox it re-send data, but in Chrome and Opera is not re-send

 

this is all in comments.php and i include it in other file

<h4 class="h3-title">Comments</h4><small><?php if (!isset($_SESSION['username'])) { echo 'You must be looged in to post comments'; } ?></small>
<hr />

<?php

// list all comments
if (isset($_GET['id']))
{
	$id = (int)$_GET['id'];

	$list_all_comments = $comments->list_comments($id);

	foreach ($list_all_comments as $comm) 
	{
		echo '<i>' . $comm['comment_author'] . ' - <small>' . date('m F Y h.m.s', $comm['comment_date']) . '</small></i><br />';
		echo '<p>' . $comm['comment_body'] . '</p><hr />';
	}
}

// show comment form only for logged in users
if (isset($_SESSION['username']))
{
	// process form data
	if (isset($_POST['comment']))
	{
		if (isset($_GET['id']))
		{
			$news_id 	= (int)$_GET['id'];
			$author 	= $_SESSION['username'];
			$body 		= trim(htmlentities($_POST['body']));
			$date 		= time();

			if (!empty($body))
			{
				// add_comment($author, $body, $date, $news_id)
				$insert_comment = $comments->add_comment($author, $body, $date, $news_id);

                                // refresh page after adding data
				echo '<meta http-equiv="refresh" content="0">';
			}
		}	
	}

?>

<form action="" method="POST">
	<textarea name="body" cols="75" rows="6" autofocus required></textarea><br />
	<input type="submit" name="comment" value="Submit">
</form>

<?php } ?>

You should be using a redirect instead of the refresh.

 

change:

// refresh page after adding data39. 
echo '<meta http-equiv="refresh" content="0">';

to:


header('location:mypage.php?id='.$news_id);

Obviously changing 'mypage.php' to your page name.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.