In order to prevent a form from being re-submitted, you could do something like this - it should be pretty self explanatory:
function uniquePost($posted) { // take some form values $description = $posted['t_betreff'].$posted['t_bereich'].$posted['t_nachricht']; // check if session hash matches current form hash if (isset($_SESSION['form_hash']) && $_SESSION['form_hash'] == md5($description) ) { // form was re-submitted return false return false; } // set the session value to prevent re-submit $_SESSION['form_hash'] = md5($description); return true; } if (isset($_POST["t_submit"]) && uniquePost($_POST)) { $ticket_query = $db->prepare("INSERT INTO `ticket` (`Absender`, `Betreff`, `Abteilung`, `Prioritat`, `Erstellt`, `Nachricht`) VALUES (:sender, :betreff, :abteilung, :priority, :datum, :nachricht)"); $ticket_query->execute(array( 'sender' => $_SESSION["id"], 'betreff' => $t_betreff, 'abteilung' => $t_bereich, 'priority' => $t_priority, 'datum' => date('d.m.Y'), 'nachricht' => $t_nachricht )); // no need to reset the post variables }
11 Years Ago
For the following code I have a problem. When i fill in the form and submit it inserts a new database value. But when I refresh the page it does it again automatically with the same data, and keeps doing it whenever i refresh. How do i stop this?
<html> <head> <title>Using Default Checkbox Values</title> </head> <body> <?php // Include our login information include('db_login.php'); $self = htmlentities($_SERVER['PHP_SELF']); echo (' <form method="POST" action="'.$self.'"> ID: <input type="text" name="id" /><br /> Name: <input type="text" name="name" /><br /> Comment: <input type="text" name="comment" /><br /> Date: <input type="text" name="date" /><br /> Other: <input type="text" name="other" /><br /> <input type="submit" value="Go!" /><br /> </form> '); if(!$_POST['name'] == ''){ $query1 = "INSERT INTO `unknown`.`comments` (`id`, `user`, `comment`, `date`, `other`) VALUES (NULL, '$_POST[name]', '$_POST[comment]', '2010-10-23', '$_POST[other]')"; $result1 = mysql_query( $query1 ); $_POST['name'] = ''; unset($_POST); } // Assign the query $query = "SELECT * FROM comments"; // Execute the query $result = mysql_query( $query ); if (!$result){ die ("Could not query the database: <br />". mysql_error( )); } // Fetch and display the results while ($result_row = mysql_fetch_row(($result))){ echo 'ID: '.$result_row[0] . '<br />'; echo 'Author: '.$result_row[1] . '<br /> '; echo 'Comment: '.$result_row[2] . '<br /> '; echo 'Date: '.$result_row[3] . '<br /> '; echo 'Other: '.$result_row[4] . '<br /><br />'; } //Close the connection mysql_close($connection); ?> </body> </html>Recommended Answers
Write something in a $_SESSION variable to detect that you already stored the form.
Jump to Post
All 3 Replies
pritaeas 2,114 ¯\_(ツ)_/¯
Moderator Featured Poster Write something in a $_SESSION variable to detect that you already stored the form.
11 Years Ago
Or use the below code to send them to a new page that says whatever you want:
<? header("Location: file.php"); ?>Edited 11 Years Ago by Danny247 because: n/a
ErlendHL 0 Junior Poster in Training
11 Years AgoYou must unset the $_POST somewhere in the end of the document (where you don't use it anymore):
<?php unset($_POST['name']) ?>Reply to this topic
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.