Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't prevent form's submission (page reload) if ViewTransitions is used #9488

Closed
1 task
slaetthult opened this issue Dec 20, 2023 · 13 comments
Closed
1 task
Labels
needs triage Issue needs to be triaged

Comments

@slaetthult
Copy link

slaetthult commented Dec 20, 2023

Astro Info

Astro                    v4.0.6
Node                     v18.18.2
System                   macOS (x64)
Package Manager          unknown
Output                   static
Adapter                  none
Integrations             @astrojs/alpinejs
                         @astrojs/partytown

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

I can't prevent form's submission if ViewTransitions is used. Or at least page gets reloaded.

<form onsubmit="return false;" method="POST">
    <button type="submit">Submit</button>
</form>

This form gets still submitted even though return false is used. Or at least page gets reloaded.
As soon as I remove ViewTransitions, everything works like expected.

Everytime I click the button, I get the following in terminal:
Bildschirmfoto 2023-12-20 um 17 04 17

What's the expected result?

Form should not get submitted if onsubmit="return false;" is used and page should not reload.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-ihjv95?file=src%2Fpages%2Findex.astro

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 20, 2023
@martrapp
Copy link
Member

martrapp commented Dec 21, 2023

Hi @slaetthult, starting with Astro v4.0.7 you can call event.preventDefault() from a submit event listener to prevent the default behavior, see #9486

@martrapp
Copy link
Member

please upgrade to 4.0.7 and try this:

---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';
---

<Layout title="Welcome to Astro.">
  <main>
    <form  method="POST">
      <button type="submit">Submit</button>
    </form>
  </main>
</Layout>
<script>document.querySelector("form").addEventListener('submit',(e)=>e.preventDefault())</script>

@slaetthult
Copy link
Author

It works, thank you a lot!

@martrapp
Copy link
Member

👍🏼 Good to hear!

@ArthurSalle
Copy link

ArthurSalle commented Nov 21, 2024

Hi @martrapp , I'm currently facing the same problem but I'm using Astro with SSR for my form submission, is there any solution like the preventDefault() method but not on the client side?
At the first page load, every thing is fine but once I submit the form and then refresh multiple time my page it submit my form and my log('here') still appears.

I'm using it like this:

---
if (Astro.request.method === "POST") {
  console.log("here");
}
---

<form method="POST">
</form>

Thanks 🙂

@martrapp
Copy link
Member

Hi @ArthurSalle 👋🏼, sadly I have no experience with SSR. But you should ask in the #support channel over at discord!

@ArthurSalle
Copy link

Ok, I'll take a look at it, thanks for your quick reply! 🚀

@CaseoJKL
Copy link

Ok, I'll take a look at it, thanks for your quick reply! 🚀

Any updates on this issue ?

@ArthurSalle
Copy link

Ok, I'll take a look at it, thanks for your quick reply! 🚀

Any updates on this issue ?

No, not yet 🤷🏼‍♂️

@CaseoJKL
Copy link

CaseoJKL commented Nov 21, 2024

https://docs.astro.build/en/recipes/build-forms-api/

I wonder why it requires A UI Framework integration installed.
Can we not do this without a UI Framework integration ?

This will fix your issue but I don't want to use a UI Framework

@ArthurSalle
Copy link

Yes you can but it seems to be a lit bit more tricky without an ui framework.

If I submit my form "normally" by clicking my submit button every thing is okay. But then if I refresh the page my form is being submitted again and again. So, I've added a button with a name and a value.

---
if (Astro.request.method === "POST") {
  console.log("here"); // execute on the first submit & every time I refresh the page

try {
    const formData = await Astro.request.formData()
    const submitBtn = formData.get('submit');

    if (submitBtn === 'submit-btn') {
        console.log('log only when btn clicked') // execute only when the submit btn is clicked and never again
    }
 
} catch {}

}

---

<form method="POST">

//input 

 <button name="submit" value="submit-btn">Submit</button>
</form>

I hope it can help you ! If not, tell me more details about your problem !

@CaseoJKL
Copy link

CaseoJKL commented Nov 25, 2024

It doesn't look reasonable as to why the issue is happening to begin with but rather a workaround.

@evokeapps
Copy link

I fixed it by adding a return Astro.redirect(REDIRECT_URL)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

5 participants