Hi there.

I am creating a social networking website and I am having an issue when posting messages on a users profile. The message posts correctly and displays correctly too but when I refresh the page a duplicate of the last message is submitted. I do not want to prevent the user from submitting more than one message in a single visit but I do need to stop messages from being posted on refresh.

I hope you can help, here is my code below.

<?php // rnmessages.php
include_once 'rnheader.php';

if (!isset($_SESSION['user']))
	die("<br /><br />You need to login to view this page");
$user = $_SESSION['user'];

if (isset($_GET['view'])) $view = sanitizeString($_GET['view']);
else $view = $user;

if (isset($_POST['text']))
{
	$text = sanitizeString($_POST['text']);

	if ($text != "")
	{
		$pm = substr(sanitizeString($_POST['pm']),0,1);
		$time = time();
		queryMysql("INSERT INTO rnmessages VALUES(NULL,
				   '$user', '$view', '$pm', $time, '$text')");
	}
}

	echo <<<_END
<form method='post' action='rnmessages.php?view=$view'>
Type here to leave a message:<br />
<textarea name='text' cols='40' rows='3'></textarea><br />
Public<input type='radio' name='pm' value='0' checked='checked' />
Private<input type='radio' name='pm' value='1' />
<input type='submit' value='Submit' name='submit' /></form>
_END;
	
	$query = "SELECT * FROM rnmessages WHERE recip='$view'
			  ORDER BY time DESC";
	$result = queryMysql($query);
	$num = mysql_num_rows($result);
	
	for ($j = 0 ; $j < $num ; ++$j)
	{
		$row = mysql_fetch_row($result);

		if ($row[3] == 0 ||
		    $row[1] == $user ||
		    $row[2] == $user)
		{
			echo date('M jS \'y g:sa:', $row[4]);
			echo " <a href='rnmessages.php?";
			echo "view=$row[1]'>$row[1]</a> ";

			if ($row[3] == 0)
			{
				echo "wrote: &quot;$row[5]&quot; ";
			}
			else
			{
				echo "whispered: <i><font
				color='#006600'>&quot;$row[5]&quot;</font></i> ";
			}

			if ($row[2] == $user)
			{
				echo "[<a href='rnmessages.php?view=$view";
				echo "&erase=$row[0]'>erase</a>]";
			}
			echo "<br>";
		}
	}
}


if (!$num) echo "<li>No messages yet</li><br />";

echo "<br><a href='rnmessages.php?view=$view'>Refresh messages</a>";
echo " | <a href='rnfriends.php?view=$view'>View $name2 friends</a>";
?>

Thanks Luke.

Recommended Answers

All 6 Replies

Member Avatar for diafol

have the form send the data to a different page and then after all the processing, redirect to the form page.

have the form send the data to a different page and then after all the processing, redirect to the form page.

How would I go about doing that, I am pretty new to PHP and most of the stuff I have done so far is through tutorials.

Thanks,

Member Avatar for diafol

Say your form is on form.php, do this:

<form action="handler.php" method="post">
  ...the html...
</form>

Send the form to handler.php. In handler php, you do all the data processing and then return to the same page or a different one with:

header("Location: http://mysite.com/form.php");

You must ensure that nothing is output to the screen or there is any html before the header(), otherwise you'll get a 'headers already sent' error.

Obviously, an error checking / validation system is req'd. You can pass error codes back to the form.php via querystring:

header("Location: http://mysite.com/form.php$err");

Where $err is the passed errorcode (e.g. $err = "?err=" . $errorcode), if there is one.

Sometimes I set errorcodes as constants, so you can add them up and extract the info without too much hassle. Your original form can have something like:

<input ... /><?php if(isset($_GET['err']) && (4 & $_GET['err'] != 0))echo " <span>Bad Email Dude</span>";?>

This assumes 4 = bad email bit.

Sorry for the late reply! That works perfectly! thank you so much!

Member Avatar for diafol

OK, mark the thread as solved.

on another note you could use javascipt to do the same thing

echo '<script>setTimeout("window.location.replace(\'edit.php\')",3000);</script>';
//3000 is 3 seconds
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.