Jump to content


Photo

Stopping POSTDATA from being resent?


  • Please log in to reply
4 replies to this topic

#1 Ghostu

Ghostu
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 17 September 2006 - 07:17 PM

I made a comment script where users can type their comment into a form and then press submit to add it to my database. It works fine except for that if a user submits a comment, but then refreshes the page, the POSTDATA is resent and then the comment is posted again. So everytime the page is refreshed the same comment is added. What can I do about this?
Heres the script in action: http://photoshopandy...ction=view&id=7
And the code:
//Comments
//The date
$date = date('F jS, Y g:i a');
//Variables
if(isset ($_POST['submit'])) {
//Simplifying the variables.
$title = $_POST['title'];
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//Checks for empty fields or invalid date.
if((empty($title)) OR (empty($name)) OR (empty($email)) OR (empty($message))) {
echo "<center><strong>Please fill in all fields!</strong></center>";
} else {
//The MySQL query which will insert content into the table.
mysql_query("INSERT INTO tutorial_comments (ID, tutorial_id, title, date, name, email, message) VALUES ('', '$id', '$title', '$date', '$name', '$email', '$message')") or die(mysql_error());
echo "<center><strong>Comment added!</strong></center>";
}
}
Thanks.


#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 07:37 PM

Instead of your 'Comment added!' line, do
header('location:wherever.php');

That should fix it

#3 Ghostu

Ghostu
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 17 September 2006 - 07:52 PM

Thanks alot, that works, but one last question how would I use that to direct them back to the page they left the comment on?
Thanks again!

#4 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 17 September 2006 - 07:54 PM

Just change the wherever.php to the name of the page you want them to go to and that should work


#5 Ghostu

Ghostu
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 17 September 2006 - 08:04 PM

Thanks man it worked, the sites dynamic so had to use some extra code like this:
$page = $_SERVER['REQUEST_URI'];
$host  = $_SERVER['HTTP_HOST'];
$extra = $page;
header("Location: http://$host/$extra");





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users