aooga Posted December 17, 2008 Share Posted December 17, 2008 when webpage is refreshed, $_POST["form"] remains "Submit" Basically I have a form on the site that refreshes the page and contains an if ($_POST["form"] == "Submit"){ // add X into database } so every time page is refreshed X is added! Is there an elegant way to avoid this? Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/ Share on other sites More sharing options...
ngreenwood6 Posted December 17, 2008 Share Posted December 17, 2008 Can you show us some code and maybe elaborate a little better on what you are trying to do? I was a little confused. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718051 Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 That's a clientside thing. Possibly sending a no-cache header might work. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718057 Share on other sites More sharing options...
aooga Posted December 17, 2008 Author Share Posted December 17, 2008 Sure File named index.php{ if ($_POST["form"] == "Submit"){ $name = $_POST['name']; $startDate = $_POST['startDate']; $endDate = $_POST['endDate']; $duration = $_POST['duration']; $type = $_POST['type']; $source = $_POST['source']; $details = $_POST['details']; $sql = " INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details) VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details') "; $query = mysql_query($sql, $con); } <form action="index.php" method="post"> <tr> <td><input type="text" name="name" value="" /></td> <td><input type="text" name="startDate" value="" /></td> <td><input type="text" name="endDate" value="" /></td> <td><input type="text" name="duration" value="" /></td> <td><input type="text" name="type" value="" /></td> <td><input type="text" name="source" value="" /></td> <td><input type="text" name="details" value="" /></td> </tr> </table> <input type="submit" name="form" value="Submit" /> </form> } After submitting 1 name, every time I refresh it is duplicated. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718060 Share on other sites More sharing options...
ngreenwood6 Posted December 17, 2008 Share Posted December 17, 2008 why dont you actually make it check the variables. something like if (($name != "") && ($startDate != "") { $sql = " INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details) VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details') "; $query = mysql_query($sql, $con); } You would have to continue on with the variable checking but basically this will not post unless something is set for the variables. The way you have it now it will post if the submit button is there. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718086 Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 Refreshing automatically resends previously posted data. That's why it's entering more stuff into db. You can check if it's set or != '' and everything else and it will still evaluate true. That's a clientside thing. Possibly sending a no-cache header might work. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718092 Share on other sites More sharing options...
aooga Posted December 17, 2008 Author Share Posted December 17, 2008 I'm confused. So what should I do? Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718098 Share on other sites More sharing options...
.josh Posted December 17, 2008 Share Posted December 17, 2008 Like I said, put at the top of your output a regular html header that specifies cache control: no-cache. That might work. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718101 Share on other sites More sharing options...
twm Posted December 17, 2008 Share Posted December 17, 2008 see if this helps: if ($_POST["form"] == "Submit"){ $name = $_POST['name']; $startDate = $_POST['startDate']; $endDate = $_POST['endDate']; $duration = $_POST['duration']; $type = $_POST['type']; $source = $_POST['source']; $details = $_POST['details']; $sql = " INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details) VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details') "; $query = mysql_query($sql, $con); unset($_POST["form"]); } <form action="index.php" method="post"> <tr> <td><input type="text" name="name" value="" /></td> <td><input type="text" name="startDate" value="" /></td> <td><input type="text" name="endDate" value="" /></td> <td><input type="text" name="duration" value="" /></td> <td><input type="text" name="type" value="" /></td> <td><input type="text" name="source" value="" /></td> <td><input type="text" name="details" value="" /></td> </tr> </table> <input type="submit" name="form" value="Submit" /> </form> } Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718236 Share on other sites More sharing options...
premiso Posted December 17, 2008 Share Posted December 17, 2008 Use header to redirect back to index.php to clear up the post data. if ($_POST["form"] == "Submit"){ $name = $_POST['name']; $startDate = $_POST['startDate']; $endDate = $_POST['endDate']; $duration = $_POST['duration']; $type = $_POST['type']; $source = $_POST['source']; $details = $_POST['details']; $sql = " INSERT INTO polyphasic (name, startDate, endDate, duration, type, source, details) VALUES ('$name', '$startDate', '$endDate', '$duration', '$type', '$source', '$details') "; $query = mysql_query($sql, $con); header("Location: index.php"); } <form action="index.php" method="post"> <tr> <td><input type="text" name="name" value="" /></td> <td><input type="text" name="startDate" value="" /></td> <td><input type="text" name="endDate" value="" /></td> <td><input type="text" name="duration" value="" /></td> <td><input type="text" name="type" value="" /></td> <td><input type="text" name="source" value="" /></td> <td><input type="text" name="details" value="" /></td> </tr> </table> <input type="submit" name="form" value="Submit" /> </form> I usually send users to a "confirmation" page so they know it was entered. Using the header redirect will clear out any post data so even if they hit back it will not re-post the data. Quote Link to comment https://forums.phpfreaks.com/topic/137420-when-webpage-is-refreshed-_postform-remains-submit/#findComment-718262 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.