[CS-FSLUG] Preventing a form re-posting PHP/MYSQL

'Mash mashdot at gmail.com
Fri Jun 16 19:49:09 CDT 2006


I am busy learning MYSQL and PHP and I have created a simple
page that displays current database entries and provides a form to
add another one on the same page.

My problem is that if I refresh the page it reposts the POST and adds the
data to the database again.

I have been looking on the net which is giving some examples
on how to get round this but would like to know if anyone has any ideas
how to prevent a form from reposting when you refresh the browser window?

I was thinking of if there was a way to input a hidden field that
would generate a (random number) in the POST string and would not allow you
to post again if the (random number) existed. Hence by refreshing the browser.
But if you hit the "Submit Memo" button again it would generate
another (random number)
thus allowing you to make another post.

Does that make sense ?

Unfortunatly this page is not live on the web but on my test server.
Below is the code I am using.

Also, any idea how to prevent somone from posting a blank data in the form ?

// Displays memotext
include 'includes/result_memo.inc.php';

<form action ="<?php echo $SERVER['PHP_SELF']; ?>" method="post">
<label>Enter new memo here:<br />
<textarea name="memotext" rows="10" cols="40">
</textarea></label><br />
<input type="submit" value="Submit memo" />

   $sql = "INSERT INTO memo set
   memotext = '$memotext',
   memodate = CURDATE(),
   memotime = CURTIME()";
   if (!mysql_query($sql)) { echo "<p>Memo addition failed ' .
mysql_error() . '</p>"; }


More information about the Christiansource mailing list