1

You can send data after form submission by including it in the form action return. This data is accessed through the $page.form store.

If you want to redirect to a different page on form submission, you redirect to a different page by throwing a Redirect object. The redirect method only has two parameters (code and location), with no options to return data (unlike fail for example).

Is there any way to return data and redirect on form submission, besides just adding it all as URL parameters?

Thanks.

CC BY-SA 4.0
1

2 Answers 2

Reset to default
4

This is not a limitation of SvelteKit. HTTP redirect consists of url and optional status. Reference Response: redirect() static method.

Is there any way to return data and redirect on form submission

If this can happen consecutively, do the redirect on the client-side. This requires JavaScript, and would therefore not work if the browser JS is not available.

<script lang="ts">
  import { goto } from '$app/navigation';
  import type { ActionData } from './$types';

  export let form: ActionData;

  // Check the form value and redirect accordingly
  $: if (form?.ok) goto('/redirect/url');
  $: if (form?.url) goto(form.url);
</script>

If you are using external DB, etc. that can store the submitted data, in the form action function

  1. INSERT into DB
  2. Throw a redirect
  3. Let the new page's server load function SELECT the data from the DB.
CC BY-SA 4.0
1
1

This is not possible. It would not work reliably, as redirecting while also keeping page state cannot be done without JavaScript. The application may also have to perform a reload after it has been updated which would interrupt this.

CC BY-SA 4.0
0

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions taggedor ask your own question.