form action on a different page with use:enhance
#10842
-
Hi, I'm using a form with an action on another page. <form
action="/somewhere/else"
method="post"
>
<button type="submit">
Submit
</button>
</form> It works fine without enhancements, I'm getting redirected to But when I add I tried using https://kit.svelte.dev/docs/form-actions#progressive-enhancement-use-enhance
<form
action="/somewhere/else"
method="post"
use:enhance={({ formElement, formData, action, cancel }) => {
return async ({ result }) => {
await applyAction(result); // ???
};
}}
>
<button type="submit">
Submit
</button>
</form> I'm only getting {
"type": "success",
"status": 200,
"data": {}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 11 replies
-
Native form submission always navigates to the route specified in the form You could choose to emulate the navigation to the <form
action="/somewhere/else"
method="post"
use:enhance={({ formElement, formData, action, cancel }) => {
return async ({ result }) => {
// emulate native browser form redirect
if (result.type === "success") {
await goto(action);
}
await applyAction(result);
};
}}
>
<button type="submit">
Submit
</button>
</form> |
Beta Was this translation helpful? Give feedback.
-
What wasn't clear to me from the documentation nor the posts I could find online is what the returned response should be from the formaction. I got this form:
To redirect by returning the following in my
Wanted to share in case anybody runs into the same issue and needs a hint. |
Beta Was this translation helpful? Give feedback.
Native form submission always navigates to the route specified in the form
action
attribute. However,use:enhance
only navigates to another page if you explicitly throw a redirect in your action in+page.server.js
.You could choose to emulate the navigation to the
action
route after a successful response with agoto
call https://kit.svelte.dev/docs/modules#$app-navigation-goto