Jump to content

Help to prevent re-sending data in comment script


Go to solution Solved by paddy_fields,

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 } ?>
  • Solution

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.

Edited by paddyfields
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.