0

So, I have a form which I submit once and redirect it withing the same page and after the refresh I execute some scripts on the data.

For ex, if my page is index.php I can summarise it like this:

<?php
if(isset($_POST['msg']))
{
  //do smth
}
$_POST=array();
?>
<html>
....
<form action="index.php" method="post">
   <input type="text" id="msg" name="msg">
   <button type="submit">Submit</button>
</form>
....
</html>

The problem is that after the first submission, the POST variable regains its initial value after every page refreshment even though I delete it every time and I don't resubmit the form.

So what should I do?

Popescu Ion
  • 142
  • 10
  • I'm not seeing any sort of redirect above – Patrick Q Sep 26 '19 at 16:23
  • try unsetting your pot variable once you've used it: unset($_POST['value']); – lewisnewson Sep 26 '19 at 16:36
  • i tried with unset, but it doesn't work either – Popescu Ion Sep 26 '19 at 16:38
  • for ex, if I print the POST variable inside the HTML, it says: "unknown variable", so it does indeed delete it, but when refreshing the page, it somehow gains its value again – Popescu Ion Sep 26 '19 at 16:40
  • @PatrickQ by redirect, I mean that the form action points to the same page – Popescu Ion Sep 26 '19 at 16:41
  • 2
    So you're submitting the form, and then literally using your browser's refresh functionality? When you do that, it usually gives you the option of re-submitting the form or not. If you are telling it to do so, well, then that's why it is. _Why_ are you refreshing the form in the first place? Also, doing `$_POST=array();` will only effect the _current_ execution. The next time the page is loaded, it's as if that never happened. – Patrick Q Sep 26 '19 at 16:53
  • Well, for the first 20 times it didn't ask me anything, now I see it does. But I still want to destroy the form data after being executed for the first time, bcz otherwise it just enters the same data in my database, and I want each user to be able to complete the form only once – Popescu Ion Sep 26 '19 at 16:56
  • on page refresh after submit, browser will ask you for Confirm Form Resubmission, because it is repeating the previous action that was to post form values to the action page, which in this case is your current (index.php) page – Umer Abbas Sep 26 '19 at 16:58
  • Of course I can verify if he already submitted it, but in that case an error box will be shown and I don't want that. – Popescu Ion Sep 26 '19 at 17:00
  • @OmarAbbas I know, but I just want to destroy the data so that I can refresh the page without asking for Confirmation – Popescu Ion Sep 26 '19 at 17:01
  • @PopescuIon you can use sessions to prevent resubmissions from same browser here is a link for you, https://stackoverflow.com/questions/35557034/how-to-restrict-a-user-from-submitting-a-form-more-than-once-in-10-mins-using-ph – Umer Abbas Sep 26 '19 at 17:02
  • Everything is ok now. I tried bolcking access with SESSIONs before but it didn't work, but now the problem is solved somehow :))) – Popescu Ion Sep 26 '19 at 17:10
  • Possible duplicate of [Simple Post-Redirect-Get code example](https://stackoverflow.com/questions/4142809/simple-post-redirect-get-code-example) – Dharman Sep 28 '19 at 16:50

0 Answers0