Jump to content

Stop flooding of form on page refresh


Dada78

Recommended Posts

I have a page that has a comment form where people can leave comments. The problem with it is that when you leave a comment, you can hit the refresh button and it will post the last comment again. So you can hit the refresh button over and over again posting the last message and flooding the comments. Is their anything I can do to prevent this from happening?

 

-Thanks

 

 

Link to comment
Share on other sites

Instead of posting the long file I will just post the relevant code.

 

The php

 

// Start code for inserting comments

$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')";
                $result=mysql_query($query) or die(mysql_error()); // run our query

// end code for inserting comments

 

 

The HTML

 

<p><span class="title">Comments...</span><table align="center" style='border-top: 1px solid #990000' border="0" cellpadding="4" cellspacing="0" width="550">
  <a name="comments"><?php show_data(); ?></a>
</table></p>

<p><form method="POST" action="display.php?id=<?php echo $row['id']."#comments"; ?>">
<table align="center" border="0" cellpadding="0" cellspacing="0" width="550">
     <tr>
        <td>
		<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
			Name: <input type="text" name="firstname" value="Anonymous" onClick="this.value=''">   Location: <input type="text" name="location" value="Anonymous" onClick="this.value=''"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" value="Submit"> <input type="reset" value="Reset"></fieldset></p></td></tr></table></form></p>

 

 

Link to comment
Share on other sites

In any situation where something is being posted to the database, upon success it should always forward to another page (I call mine success.php). Then have that page automatically forward back to the original page after a set period of time (see code below). You can also add a link that they can click to return to the original page if they dont want to wait. This way if they hit refresh on either the success page, or the page they return back to, the data wont be re-posted.

 

You can use this code to forward the user from the success page after a preset period of time (in this case 5 seconds):

 

header('Refresh: 5; url=' . [enter the URL they will be forwarded to]);

 

Make sure you put this at the top of your code, with no whitespace or any output to the browser before it. Replace the part in the squre brackets with the URL you want them to be forwarded to.

Link to comment
Share on other sites

Yes, but there is no way around that. Its the way most sites do it (although AJAX functions remove the necessity for this).

 

You just put a message on the success page that says something like

 

"message successfully posted. You will be forwarded back to the page you were previously on shortly, or click <a href="something.php">here</a> if you dont want to wait."

 

You must have seen this on a site somewhere in the past.

Link to comment
Share on other sites

setup a cookie/POST with a timeout value;

 

in your form (note this for html code)

<INPUT TYPE=HIDDEN NAME="timeout" value="<?=time()+5;?>">
[code]

now in yer processing ya can do

[code]
if(isset($_POST['timeout'])) {
    if(time() < $_POST['timeout'])
      unset($_POST);
}

 

which invalidates the whole re-submission.[/code][/code]

Link to comment
Share on other sites

setup a cookie/POST with a timeout value;

 

in your form (note this for html code)

<INPUT TYPE=HIDDEN NAME="timeout" value="<?=time()+5;?>">
[code]

now in yer processing ya can do

[code]
if(isset($_POST['timeout'])) {
    if(time() < $_POST['timeout'])
      unset($_POST);
}

 

which invalidates the whole re-submission.[/code][/code]

 

Ok I tried this and it didn't work. I think I might have placed the if(isset($_POST['timeout'])) code in the wrong place. Where exactly does that need to go?

 

 

Link to comment
Share on other sites

if((isset($_POST['timeout']) && (time() > $_POST['timeout'])) {
$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')";
                $result=mysql_query($query) or die(mysql_error()); // run our query

// end code for inserting comments
}

 

the html

<p><form method="POST" action="display.php?id=<?php echo $row['id']."#comments"; ?>">INPUT TYPE=HIDDEN NAME="timeout" value="<?=time()+5;?>">

Link to comment
Share on other sites

Ok this still isn't working the code above was throwing an error because of the bracket at the end of this line

 

if((isset($_POST['timeout']) && (time() > $_POST['timeout'])) {

 

So I did it like this.

 

// Start code for inserting comments

if(isset($_POST['timeout'])) {
    if(time() < $_POST['timeout'])
      unset($_POST);
$firstname = $_POST['firstname'];
$location = $_POST['location'];
$comment_date = date("m-d-Y");
$comment = $_POST['comment'];
$id = $_POST['id'];

// setup our query
$query ="INSERT INTO comments (firstname, location, comment_date, comment, id) VALUES ('$firstname', '$location', '$comment_date', '$comment', '$id')";
                $result=mysql_query($query) or die(mysql_error()); // run our query

// end code for inserting comments
}

 

Then in the form I did this.

 

<fieldset class="fieldset">
			<legend class="legend"><strong>Leave A Comment</strong></legend>
			      <input type="hidden" name="id" value="<? echo $row['id']; ?>" />
                                      <input type="hidden" name="timeout" value="<?php time()+5; ?>">
			Name: <input type="text" name="firstname" value="Anonymous" onClick="this.value=''">   Location: <input type="text" name="location" value="Anonymous" onClick="this.value=''"> <br>Comment (Max 255 characters): <br><textarea name="comment" rows="5" style="width: 500px;" onkeydown="javascript:limittext(this.form.comment);"></textarea>
<input type="submit" value="Submit"> <input type="reset" value="Reset"></fieldset>

 

 

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.