myristate Posted March 19, 2011 Share Posted March 19, 2011 Hi there, I wrote a pretty simple html form that inputs data into my database. I've included a truncated version of it, There are a lot more fields. Basically when i hit the submit it processes input.php but after its been successful the page is still at input.php in the browser Is there anyway to take the page back to the page i was using to input data <form action="insert.php" method="post"> <p>Adornment Name: <br> <input type="text" name="name"> <br> <br> Quality: <br> <select name="quality"> <option value="Other">Other Not Listed</option> <option value="Superior">Superior</option> <option value="Greater">Greater</option> <option value="Lesser">Lesser</option> <option value="Raid">Raid</option> <option value="Faction">Faction</option> </select> </form> include 'db_connect.php'; $sql="INSERT INTO $db_table (name, quality) VALUES ( '$_POST[name]', '$_POST[quality]')"; if (!mysql_query($sql,$db)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($db) ?> Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/ Share on other sites More sharing options...
Krash Posted March 19, 2011 Share Posted March 19, 2011 Assuming you want the user to see the "1 record added" message, put a Back button at the end of the code to return to the input form - <head> <script type="text/javascript"> function goBack() { window.history.back() } </script> </head> <input type="button" value="Back" onclick="goBack()" /> Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189377 Share on other sites More sharing options...
floridaflatlander Posted March 19, 2011 Share Posted March 19, 2011 Ok I've been up since 5am but why do you have redirect in your title? Aren't you wanting it to stay on the same page/file? To me something is missing, from what I see it should stay on insert.php after you have inserted your info. You have something redirecting your page. check that out then maybe use header() if all else fails header("Location: insert.php"); exit(); mysql_close($db) ?> And "echo "1 record added";" doesn't mean one record was added, it just means the script ran. I use a simple sql query that diplays the last record added to let me know I the info entered and that it was entered correctly. Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189448 Share on other sites More sharing options...
myristate Posted March 19, 2011 Author Share Posted March 19, 2011 The 1 record added was actually part of a code snippet i had used that i never removed its not actually meant to be there. I guess i dint explain it very well. rather than having the page go to insert.php on submit to process it i was hoping there was a way to keep it on add.php (even though it still needs to process insert.php)I did think about using the get method instead of put but i have long variables and some things i don't want visible in the url path. I had read about the header redirect thing, However wouldn't that redirect before it processes the php in the page Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189456 Share on other sites More sharing options...
floridaflatlander Posted March 19, 2011 Share Posted March 19, 2011 Can you still use header("Location: add.php"); exit(); mysql_close($db) ?> at the end of your script to redirect to the add.php page? Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189519 Share on other sites More sharing options...
Krash Posted March 19, 2011 Share Posted March 19, 2011 You can also do this - mysql_close($db) echo ' <script>window.location = "http://www.yourdomain.com/...../add.php"</script> '; ?> You need some error handlers to inform the user if/why the db update fails. Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189616 Share on other sites More sharing options...
myristate Posted March 19, 2011 Author Share Posted March 19, 2011 The header redirect worked great, Guess i should of tried it after reading about it. You mention possible error handlers? have you got an example of what you mean to clarify Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189665 Share on other sites More sharing options...
Krash Posted March 20, 2011 Share Posted March 20, 2011 You should anticipate input or db errors - missing fields, invalid inputs, server errors, etc. - and have insert.php echo error messages so the user knows the update failed and why. If the script just dies when an error occurs, it will leave the user hanging and they won't know how to continue. Quote Link to comment https://forums.phpfreaks.com/topic/231054-redirecting-page-after-form-post-method/#findComment-1189785 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.