Skip to content

form action on a different page with use:enhance #10842

Answered by eltigerchino
SteelAlloy asked this question in Q&A
Discussion options

You must be logged in to vote

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 a goto call https://kit.svelte.dev/docs/modules#$app-navigation-goto

<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">…

Replies: 2 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by SteelAlloy
Comment options

You must be logged in to vote
7 replies
@budivoogt
Comment options

@eltigerchino
Comment options

@budivoogt
Comment options

@eltigerchino
Comment options

@budivoogt
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants