2

I'm currently creating a form on a page in OctoberCMS that executes a PHP code block.

HTML/Twig:

{{ form_open({ request: 'onHandleForm' }) }}
    Please enter a string: <input type="text" name="value"/>
    <input type="submit" name='submitform' value="Submit me!"/>
{{ form_close() }}
<p>Last submitted value: {{ lastValue }}</p>

PHP:

function onHandleForm()
{
    $this['lastValue'] = post('value');
}

As you can see this is very simple, PHP creates a twig tag with a value equal to the user input which is then output in the HTML.

However, every time I try to reload the page after posting something I get the error:

The page that you're looking for used information that you entered. Returning to that page might cause any action you took to be repeated. Do you want to continue?`

I've tried every method I can find but I can't get that error to stop coming up. I know it has something to do with the server trying to repost the same data when you reload the page but I can't for the life of me figure out how to fix it, I've tried every single method of reloading the page using PHP that I can find and none of them seem to work.

Please don't close this as a duplicate, if any of the existing posts helped me I wouldn't be making a new one.

Update: I'm not sure but it seems like reloading the page is not getting rid of the error: I just added onsubmit="window.location.reload()" to my form and now when I submit it reloads the page but the error persists.

Mark Kramer
  • 3,134
  • 7
  • 34
  • 52
  • I'm not sure if this'll work, but instead of replying with 200 OK and content to a POST, try telling them to go to another page for the response. I think either 301 or 302 will do that, but I'm not certain. They'll get that with a GET, and refreshing the page won't require resending the POST to get the updated content. – Nic Feb 18 '17 at 01:26
  • *"Please don't close this as a duplicate, if any of the existing posts helped me I wouldn't be making a new one."* - Being which ones? You want to redirect after post; I'm sure I can find quite a few that would be considered as duplicates. – Funk Forty Niner Feb 18 '17 at 01:34
  • Yes, I found many of them and the answers were not specific enough to help. Also I couldn't find any that incorportated Twig. Most of them use PHP files, which is an option I don't have working with OctoberCMS and I've been unable to successfully adapt them to work with my code so I'm hoping somebody will know a method that will actually work with my code because I've now spent 3 hours trying to fix this one little thing. – Mark Kramer Feb 18 '17 at 01:37
  • Have you tried any of these in here? http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php or used error reporting? http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Feb 18 '17 at 01:39
  • Yes, I've tried all of them. Of course, almost none of the answers provide full code examples, just out-of-context snippets that I don't know how to use in my own code so I could just be using the snippets incorrectly, I wouldn't know, I just started using PHP. Also, don't think I've used error reporting yet though, I'll have to look into that. – Mark Kramer Feb 18 '17 at 01:40
  • ok Mark. Try that, maybe something else will come of it. – Funk Forty Niner Feb 18 '17 at 01:42
  • You are reloading and `POST`ing again. You have to redirect to a different page. – Robert Feb 18 '17 at 02:12
  • But I don't want it to go to a different page. That's the point. So most people say to redirect to the same page, but I can't figure out how to get that to work. – Mark Kramer Feb 18 '17 at 02:16
  • So, if you want to *reload* the page why do you chose to do with ajax?, why do you need to reload the page? – OsDev Feb 18 '17 at 05:09

1 Answers1

4

You can refresh the page from the ajax handler inside controller if you return this:

return redirect()->refresh();
dragontree
  • 1,709
  • 11
  • 14