0

I build a page to modify a product. So I have a form with product data.

I added a button that, when we click on it, does some stuff in Ajax. At the end of the treatment, I just want to refresh my page (because I display a flash message of confirmation). In short, I just want to show my page (not submit it).

This treatment is completely something else than modifying product data.

I tried these different solution :

window.location.href = window.location.href
// or
window.location.reload(true);
// or
window.location = window.location.href;
// or
window.location.href = window.location.protocol +'//'+ window.location.host + window.location.pathname;
// or
location.reload();

Everytime, it submits my form and my product is updated !!

But I don't send any post data. I just want to do the same action than if I push F5 on my keyboard = show my form (not submit it).

Any idea ?

EDIT

Don't forget to add <button type="button" ... or it's a submit button by default.

Rookie mistake...

Another solution is to display the confirmation message with jQuery without refreshing the page.

Eve
  • 776
  • 2
  • 11
  • 32
  • 2
    Seems like showing your message in a modern way would be easier (and a better UX). – isherwood Mar 22 '19 at 15:30
  • 1
    What type does your button have? – isherwood Mar 22 '19 at 15:31
  • 1
    _"At the end of the treatment, I just want to refresh my page (because I display a flash message of confirmation)"_ ... you mean Adobe Flash? Please tell me it's not that. But anyway, regardless of how you've implemented it, you don't need to refresh in order to display a message...just use some JavaScript (to either display some HTML which looks like a popup box, or even just use `alert()`). – ADyson Mar 22 '19 at 15:33
  • 1
    As to why refreshing submits your form, it's not clear without seeing more code...but possibly your form's default action is GET? Although AFAIK a reload still shouldn't cause any form to submit....unless I'm wrong, that sounds pretty strange to me – ADyson Mar 22 '19 at 15:34
  • @isherwood & @ADyson : Yes I should display my message with jQuery, maybe it's better... I forgot to add `type="button"` in my `button` tag. It seems that if we don't add this, it's a submit button by default. Sorry, it was Friday and I was tired lol. Rookie mistake... Thank you for your answers. – Eve Mar 25 '19 at 07:20
  • 1
    Possible duplicate of [Disable form auto submit on button click](https://stackoverflow.com/questions/9824808/disable-form-auto-submit-on-button-click) – leonheess Aug 07 '19 at 13:50

2 Answers2

0

Don't forget to add <button type="button" ... or it's a submit button by default.

Rookie mistake...

Another solution is to display the confirmation message with jQuery without refreshing the page.

Eve
  • 776
  • 2
  • 11
  • 32
-1

Your form may be submitting because the default type for a button is submit. Try changing it to button.

isherwood
  • 58,414
  • 16
  • 114
  • 157