I'm working on a very simple CMS system where the admin can enter data into the html input's and then the php turns that data from the database and displays a table. So far so good. It's a very simple scipt. On the post.php page, I have an if statement where it will display a message saying the data was correctly added. Now here is the problem. If the admin wants to enter another row in the database he can't click refresh on the browser or it will re-enter the same row. Is there a way that after, say 3 seconds, the php code will go back to the form? Here is the code for the index: <?php $mysqli = new mysqli("localhost", "root", "", "pennypicks"); // Create the query $query = "SELECT sku, name, price FROM recent ORDER by name"; // Send the query to MySQL $result = $mysqli->query($query, MYSQLI_STORE_RESULT); // Iterate through the result set while(list($sku, $name, $price) = $result->fetch_row()) printf("(%s) %s: \$%s <br />", $sku, $name, $price); ?> PHP: and for the post.php page: <?php if (isset($_POST['sku'])) { mysql_connect("localhost", "root", ""); mysql_select_db("pennypicks"); $sku = addslashes($_POST['sku']); $name = addslashes($_POST['name']); $price = addslashes($_POST['price']); $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');"); if ($result) { echo "Thanks for posting - Update added to mainpage!"; exit; } else { echo "There was an error adding your entry - please try again, filling in all fields."; } } ?> PHP: Again, when I hit refresh once the message shows, it adds in the same row, but only shows the "Thanks for posting - Update added to mainpage!". When I hit refresh, I want it to display the form again. Help would be appreciated!
If you do not want a delay: // Replace "index.php" with the page to redirect to. header("Location: index.php"); PHP: If you do want a delay: echo "<script type='text/javascript'> function delay() { setTimeout(\"window.location = 'index.php'\", 1000); } document.onload = delay(); </script>"; PHP:
I don't think you understand. There is no delay. After you submit the data, it displays the message. When you try and refesh the page (to go back to the form to insert another row) it just adds a duplicate row to the database that you just typed in, instead of bringing you back to the form so the admin can then insert another row.
try this: $ref = $_SERVER['HTTP_REFERER']; header("Location: ".$ref); exit; PHP: This will send them back to where they submitted the form from.
Right after all the processing from the submission is done. Where you would normally echo out the confirmation message.
So something like this? <?php if (isset($_POST['sku'])) { mysql_connect("localhost", "root", ""); mysql_select_db("pennypicks"); $ref = $_SERVER['HTTP_REFERER'] $sku = addslashes($_POST['sku']); $name = addslashes($_POST['name']); $price = addslashes($_POST['price']); $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');"); if ($result) { echo "; header("Location: ".$ref); exit;"; } else { echo "There was an error adding your entry - please try again, filling in all fields."; } } ?> PHP:
This: <?php if (isset($_POST['sku'])) { mysql_connect("localhost", "root", ""); mysql_select_db("pennypicks"); $ref = $_SERVER['HTTP_REFERER']; $sku = addslashes($_POST['sku']); $name = addslashes($_POST['name']); $price = addslashes($_POST['price']); $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');"); if ($result) { header("Location: ".$ref); exit; } else { echo "There was an error adding your entry - please try again, filling in all fields."; } } ?> PHP:
Not working, it's doing exactly what it was before and displays this: Warning: Cannot modify header information
That's because you used echo() before header(). Try this: <?php if (isset($_POST['sku'])) { mysql_connect("localhost", "root", ""); mysql_select_db("pennypicks"); $ref = $_SERVER['HTTP_REFERER']; $msg = "<script type='text/javascript'> function delay() { setTimeout(\"window.location = 'index.php'\", 3000); } document.onload = delay(); </script> Thanks for posting - Update added to mainpage!"; $sku = addslashes($_POST['sku']); $name = addslashes($_POST['name']); $price = addslashes($_POST['price']); $result = mysql_query("INSERT INTO recent (sku, name, price) VALUES ('$sku', '$name', '$price');"); if ($result) { echo $msg; exit; } else { echo "There was an error adding your entry - please try again, filling in all fields."; } } ?> PHP:
Awesome thanks a ton! Now when I also press the back button it doesn't duplicate it either! Thanks a ton!