Jump to content

Error: Submitting Form Multiple Times


unsider

Recommended Posts

I am running Safari on OS X, and I have designed a simple little comment page, with the basic variables, $date, $text, $session_username, etc...

 

Well when the page is reloaded, the form is submitted twice, and this is the problem.

 

How would I go about fixing this error, so that the user an reload the page without the form being submitted twice?

 

If you needs samples of my code, just let me know.

Link to comment
Share on other sites

This bit of code, allows the user to input the information into the DB, then it outputs what they have added.

When the page is reloaded, the data is then displayed a second time.

I've discovered if you add a question mark to the end before the reload it does not resend the form again.

 

http://example.com/index.php?

 

		
	if (!@mysql_select_db("//working database")) {
		echo 'Unable to locate the ' . 'database.';
}

	if(isset($_POST['commenttext'])) {
		$commenttext = $_POST['commenttext'];
		$name = $_POST['name'];
		$email = $_POST['email'];

	$sql = "INSERT INTO comments SET
		commenttext ='$commenttext',
		commentdate =NOW(),
		name ='$name',
		email='$email'";


	if (!@mysql_query($sql)) {
		echo 'Error adding query: ' . mysql_error();
}
}

		echo 'The comments listed in the database...<br>';

	$result = @mysql_query('SELECT commenttext, commentdate, name, email FROM comments');
		if (!$result) {
			echo 'Error performing query: ' . mysql_error();
}

echo '<table>';
$tr=1;
while ($row = mysql_fetch_array($result)) {
echo '<tr class="tr' . $tr . '"><td>' . $row["name"] . ' says: <br>' . $row['commentdate'] . '<br>' . $row['commenttext'] . '</td></tr>'; 

if($tr==1){
	$tr=2;
}
else if($tr==2){
	$tr=1;
}
}
echo '</table>';

echo '<br>';	

		echo '<a href="' . $_SERVER['PHP_SELF'] . '?addcomment=1">Add comment!</a>';

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.