unsider Posted February 24, 2008 Share Posted February 24, 2008 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. Quote Link to comment Share on other sites More sharing options...
tippy_102 Posted February 24, 2008 Share Posted February 24, 2008 You need to test to see if the data bas been entered. Post your code so we can see how you did things, and we'll try to help. Quote Link to comment Share on other sites More sharing options...
unsider Posted February 24, 2008 Author Share Posted February 24, 2008 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>'; Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted February 24, 2008 Share Posted February 24, 2008 I typically use header("location:someurl.php"); to leave the page after the form is submitted. another alternative might be to use SESSIONs to keep track of which pages the user has already commented on. Quote Link to comment Share on other sites More sharing options...
unsider Posted February 24, 2008 Author Share Posted February 24, 2008 That's a good point actually, I think I'll look into sessions, and only allow the user to post every so many minutes/seconds. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.