Answers.Body: <p>The redirect url always needs to be hashed:</p>
  
  <p><a href="https://docs.craftcms.com/v3/dev/functions.html#redirectinput" rel="nofollow noreferrer">https://docs.craftcms.com/v3/dev/functions.html#redirectinput</a></p>
  
  <p>In Twig you can use <code>{{ redirectInput(url) }}</code> which is a shortcut of</p>
  
  <pre><code>&lt;input type="hidden" name="redirect" value="{{ url|hash }}"&gt;
  </code></pre>
  
  <p>But if you need the same in a PHP code you can use</p>
  
  <pre><code>Craft::$app-&gt;getSecurity()-&gt;hashData($url)
  </code></pre>
  
  <p>based on the code how the Twig filter defined in <em>craftcms/cms/src/web/twig/Extension.php</em>.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-31T17:35:45.900Z
Answers.Text: One problem, though. Now after the value of the `<input name='redirect'>` is changed, every time I leave the page, I receive `This page is asking you to confirm that you want to leave - data you have entered may not be saved.`. Is there a way to solve it?
Answers.UserDisplayName: anvar
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-31T18:02:40.190Z
Answers.Text: Addition to my previous comment: this behavior is observable only for certain entries, but I could not find any system. Two entries have got exactly the same set of fields, but content is different. One is triggering the alert, another don't.
Answers.UserDisplayName: anvar
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-04T21:43:15.560Z
Answers.Text: The redirect should be a hidden input and the value is always the given url prefixed with a hash when you check the source in a browser. The other half sounds unrelated to your original question, but this is definitely not how the redirection works. I would look for a script or anything else which changes your form data. Check your headers sent to the action.
Answers.UserDisplayName: terdelyi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-27T17:47:14.017Z
Answers.LastActivityDate: 2019-07-27T17:47:14.017Z
Answers.UserDisplayName: terdelyi
Body: <p>The question was already asked (<a href="https://craftcms.stackexchange.com/questions/20084/possible-to-redirect-elsewhere-after-saving-an-entry">Possible to redirect elsewhere after saving an entry?</a>) but this was about Craft <code>2.x</code>.</p>
  
  <p>Up until now I used the following code in my plugin (I took it from one of the comments from the same question):</p>
  
  <pre><code>Craft::$app-&gt;getView()-&gt;hook('cp.entries.edit', function (array &amp;$context) {
      if (isset ($context['section']['type']) &amp;&amp; $context['section']['type'] === 'single')
      {
          Craft::$app-&gt;getView()-&gt;registerJs("
              $(function () {
                  $('input[name=\"redirect\"]').val('/');
              });
          ");
      }
  });
  </code></pre>
  
  <p>The code did the job, but after I updated to <code>3.1.32.1</code>, it stopped working, every time after entry was saved the following exception was thrown:</p>
  
  <blockquote>
    <p>HTTP 400 – Bad Request – yii\web\BadRequestHttpException<br>
    Request contained an invalid body param</p>
  </blockquote>
  
  <p>Stacktrace:</p>
  
  <pre><code>yii\web\BadRequestHttpException: Request contained an invalid body param in .../vendor/craftcms/cms/src/web/Request.php:651
  Stack trace:
  #0 .../vendor/craftcms/cms/src/web/Controller.php(314): craft\web\Request-&gt;getValidatedBodyParam('redirect')
  #1 .../vendor/craftcms/cms/src/controllers/EntriesController.php(595): craft\web\Controller-&gt;redirectToPostedUrl(Object(craft\elements\Entry))
  #2 [internal function]: craft\controllers\EntriesController-&gt;actionSaveEntry()
  #3 .../vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
  #4 .../vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction-&gt;runWithParams(Array)
  #5 .../vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller-&gt;runAction('save-entry', Array)
  #6 .../vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller-&gt;runAction('save-entry', Array)
  #7 .../vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module-&gt;runAction('entries/save-en...', Array)
  #8 .../vendor/craftcms/cms/src/web/Application.php(565): craft\web\Application-&gt;runAction('entries/save-en...', Array)
  #9 .../vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application-&gt;_processActionRequest(Object(craft\web\Request))
  #10 .../vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application-&gt;handleRequest(Object(craft\web\Request))
  #11 .../web/index.php(21): yii\base\Application-&gt;run()
  #12 {main}
  </code></pre>
  
  <p>Is there any chance to make it work again on <code>3.1.32</code>?</p>
  
Tags: <control-panel><redirect><saveentry>
Title: Redirect after saving an entry
ThreadText: # Redirect after saving an entry
  
  (Asked by: username_0 on 2019-06-27)
  
  The question was already asked ([Possible to redirect elsewhere after saving an entry?](https://craftcms.stackexchange.com/questions/20084/possible-to-redirect-elsewhere-after-saving-an-entry)) but this was about Craft `2.x`.
  
  Up until now I used the following code in my plugin (I took it from one of the comments from the same question):
  
  ```
  Craft::$app->getView()->hook('cp.entries.edit', function (array &$context) {
      if (isset ($context['section']['type']) && $context['section']['type'] === 'single')
      {
          Craft::$app->getView()->registerJs("
              $(function () {
                  $('input[name=\"redirect\"]').val('/');
              });
          ");
      }
  });
  
  ```
  
  The code did the job, but after I updated to `3.1.32.1`, it stopped working, every time after entry was saved the following exception was thrown:
  
  > HTTP 400 – Bad Request – yii\web\BadRequestHttpException  
  > Request contained an invalid body param
  
  Stacktrace:
  
  ```
  yii\web\BadRequestHttpException: Request contained an invalid body param in .../vendor/craftcms/cms/src/web/Request.php:651
  Stack trace:
  #0 .../vendor/craftcms/cms/src/web/Controller.php(314): craft\web\Request->getValidatedBodyParam('redirect')
  #1 .../vendor/craftcms/cms/src/controllers/EntriesController.php(595): craft\web\Controller->redirectToPostedUrl(Object(craft\elements\Entry))
  #2 [internal function]: craft\controllers\EntriesController->actionSaveEntry()
  #3 .../vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
  #4 .../vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
  #5 .../vendor/craftcms/cms/src/web/Controller.php(109): yii\base\Controller->runAction('save-entry', Array)
  #6 .../vendor/yiisoft/yii2/base/Module.php(528): craft\web\Controller->runAction('save-entry', Array)
  #7 .../vendor/craftcms/cms/src/web/Application.php(297): yii\base\Module->runAction('entries/save-en...', Array)
  #8 .../vendor/craftcms/cms/src/web/Application.php(565): craft\web\Application->runAction('entries/save-en...', Array)
  #9 .../vendor/craftcms/cms/src/web/Application.php(281): craft\web\Application->_processActionRequest(Object(craft\web\Request))
  #10 .../vendor/yiisoft/yii2/base/Application.php(386): craft\web\Application->handleRequest(Object(craft\web\Request))
  #11 .../web/index.php(21): yii\base\Application->run()
  #12 {main}
  
  ```
  
  Is there any chance to make it work again on `3.1.32`?
  
  **Tags:** control-panel, redirect, saveentry
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-07-27. Score: 1 | Accepted answer
  
  The redirect url always needs to be hashed:
  
  <https://docs.craftcms.com/v3/dev/functions.html#redirectinput>
  
  In Twig you can use `{{ redirectInput(url) }}` which is a shortcut of
  
  ```
  <input type="hidden" name="redirect" value="{{ url|hash }}">
  
  ```
  
  But if you need the same in a PHP code you can use
  
  ```
  Craft::$app->getSecurity()->hashData($url)
  
  ```
  
  based on the code how the Twig filter defined in *craftcms/cms/src/web/twig/Extension.php*.
  
  > **Comments:**
  > **username_0**: One problem, though. Now after the value of the `<input name='redirect'>` is changed, every time I leave the page, I receive `This page is asking you to confirm that you want to leave - data you have entered may not be saved.`. Is there a way to solve it?  
  > **username_0**: Addition to my previous comment: this behavior is observable only for certain entries, but I could not find any system. Two entries have got exactly the same set of fields, but content is different. One is triggering the alert, another don't.  
  > **username_1**: The redirect should be a hidden input and the value is always the given url prefixed with a hash when you check the source in a browser. The other half sounds unrelated to your original question, but this is definitely not how the redirection works. I would look for a script or anything else which changes your form data. Check your headers sent to the action.  
  
  ---
  
  