Answers.Body: <p>The easiest way to do this would be to use the <a href="https://putyourlightson.com/plugins/sprig#sprig-redirecturl" rel="nofollow noreferrer"><code>sprig.redirect(url)</code></a> function.</p>
  <pre><code>{% if success %}
      {% do sprig.redirect('/new/page') %}
  {% else %}
      ...
  {% endif %}
  </code></pre>
  <p>Alternatively, you can follow the pattern used in the <a href="https://github.com/putyourlightson/craft-sprig-core/blob/407a2abbfbb352cce963936401b35c693295dc9b/src/base/Component.php#L132-L135" rel="nofollow noreferrer"><code>redirect()</code></a> method, placed inside your custom module controller, to achieve this.</p>
  <pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;getResponse()-&gt;getHeaders()-&gt;set('HX-Redirect', $url);
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-07T18:51:34.263Z
Answers.LastActivityDate: 2023-06-07T18:51:34.263Z
Answers.UserDisplayName: Ben Croker
Answers.Body: <p>I am wondering if the best way to do this is to set a variable (lastStep) to true for example on last &quot;continue click&quot; and then check for that value and submit success. That way you avoid redirecting after after every &quot;continue click&quot; in your multipage form. I am not sure how you are doing the multipage form. If you are conditionally showing fields based on the step you are on then a hidden input like this would work:</p>
  <pre><code>&lt;input type=&quot;hidden&quot; name=&quot;lastStep&quot; value=&quot;true&quot;&gt;
  </code></pre>
  <p>Then have this in the top of your component:</p>
  <pre><code>{% set lastStep = lastStep ?? false %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-06-07T16:21:16.333Z
Answers.LastActivityDate: 2023-06-07T16:30:59.063Z
Answers.LastEditDate: 2023-06-07T16:30:59.063Z
Answers.UserDisplayName: PhilipBoomy
Body: <p>I have a multi-pages form which is submitting using <a href="https://putyourlightson.com/plugins/sprig" rel="nofollow noreferrer">Sprig plugin</a>.
  The action is targeting a controller in a custom module.
  Each step of the form is loading using the current step stored in session.</p>
  <p>The end of this controller is returning the URL to redirect: <code>return $this-&gt;redirect($entry-&gt;url)</code>;</p>
  <p>However, the redirect does not happen, and the form is reloaded instead of the doing the redirect.</p>
  <p>I tried to edit the end of the controller to this: <code>return $this-&gt;asSuccess(true);</code>
  Then, catch the success on front-end, before to redirect from the template using <code>{% redirect %}</code> or <code>{% do sprig.redirect(entry.url) %}</code>.
  But as I have a multi-pages form, the success is returned at every step, so the check doesn't work on final submit.</p>
  <p>I also tried to add the classic <code>{{ redirectInput(entry.url) }}</code> on the last page of the form, but the direct also does not happen, and redirect to the same page.</p>
  <p>How to make the redirect works after submitting the last step of this form (supposing there's no validation error)?</p>
  
Tags: <modules><controller><redirect><plugin-sprig>
Title: Sprig: how to redirect to a URL after submitting to a custom controller?
ThreadText: # Sprig: how to redirect to a URL after submitting to a custom controller?
  
  (Asked by: username_0 on 2023-06-07)
  
  I have a multi-pages form which is submitting using [Sprig plugin](https://putyourlightson.com/plugins/sprig).
  The action is targeting a controller in a custom module.
  Each step of the form is loading using the current step stored in session.
  
  The end of this controller is returning the URL to redirect: `return $this->redirect($entry->url)`;
  
  However, the redirect does not happen, and the form is reloaded instead of the doing the redirect.
  
  I tried to edit the end of the controller to this: `return $this->asSuccess(true);`
  Then, catch the success on front-end, before to redirect from the template using `{% redirect %}` or `{% do sprig.redirect(entry.url) %}`.
  But as I have a multi-pages form, the success is returned at every step, so the check doesn't work on final submit.
  
  I also tried to add the classic `{{ redirectInput(entry.url) }}` on the last page of the form, but the direct also does not happen, and redirect to the same page.
  
  How to make the redirect works after submitting the last step of this form (supposing there's no validation error)?
  
  **Tags:** modules, controller, redirect, plugin-sprig
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-06-07. Score: 1 | Accepted answer
  
  The easiest way to do this would be to use the [`sprig.redirect(url)`](https://putyourlightson.com/plugins/sprig#sprig-redirecturl) function.
  
  ```
  {% if success %}
      {% do sprig.redirect('/new/page') %}
  {% else %}
      ...
  {% endif %}
  
  ```
  
  Alternatively, you can follow the pattern used in the [`redirect()`](https://github.com/putyourlightson/craft-sprig-core/blob/407a2abbfbb352cce963936401b35c693295dc9b/src/base/Component.php#L132-L135) method, placed inside your custom module controller, to achieve this.
  
  ```php
  Craft::$app->getResponse()->getHeaders()->set('HX-Redirect', $url);
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2023-06-07. Score: 2
  
  I am wondering if the best way to do this is to set a variable (lastStep) to true for example on last "continue click" and then check for that value and submit success. That way you avoid redirecting after after every "continue click" in your multipage form. I am not sure how you are doing the multipage form. If you are conditionally showing fields based on the step you are on then a hidden input like this would work:
  
  ```
  <input type="hidden" name="lastStep" value="true">
  
  ```
  
  Then have this in the top of your component:
  
  ```
  {% set lastStep = lastStep ?? false %}
  
  ```
  
  
  ---
  
  