954,597 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to stop my form from submitting when I refresh the page?

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("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:
<textarea name='text' cols='40' rows='3'></textarea>
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 "";
		}
	}
}


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

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


Thanks Luke.

AdriftUniform
Light Poster
39 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 
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,

AdriftUniform
Light Poster
39 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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

AdriftUniform
Light Poster
39 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

OK, mark the thread as solved.

diafol
Rhod Gilbert Fan (ardav)
Moderator
7,796 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
 

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
itisnot_me
Posting Whiz in Training
252 posts since May 2009
Reputation Points: 12
Solved Threads: 6
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: