Answers.Body: <p>You may be able to use the <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button" rel="nofollow noreferrer">browser-native <code>&lt;button type=&quot;submit&quot;&gt;</code> feature</a>!</p>
  <p>Check this out:</p>
  <pre><code>&lt;form method=&quot;POST&quot;&gt;
    {{ csrfInput() }}
    {{ actionInput('commerce/carts/update-cart') }}
  
    {# ...your existing form controls... #}
  
    &lt;button
      type=&quot;submit&quot;
      name=&quot;redirect&quot;
      value=&quot;{{ 'update/path/target' | hash  }}&quot;&gt;Update Quantities&lt;/button&gt;
  
    &lt;button
      type=&quot;submit&quot;
      name=&quot;redirect&quot;
      value=&quot;{{ 'checkout/path/target' | hash  }}&quot;&gt;Checkout&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  <p>The only peculiar thing to note here is the direct use of Craft's <a href="https://craftcms.com/docs/3.x/dev/filters.html#hash" rel="nofollow noreferrer"><code>hash</code> filter</a>—ultimately the same functionality that <code>redirectInput()</code> helper provides, as a means to prevent tampering with the values and exposing your app to remote code execution vulnerabilities!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-17T18:34:14.857Z
Answers.Text: Thanks! This works like a charm. For anyone else wondering the `hash` filter is necessary. If you don't do it you'll get `HTTP 400 – Bad Request`.
Answers.UserDisplayName: Yawner
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-03-17T17:38:52.847Z
Answers.LastActivityDate: 2022-03-18T04:47:56.563Z
Answers.LastEditDate: 2022-03-18T04:47:56.563Z
Answers.UserDisplayName: August Miller
Body: <p>I'm building a basic cart page in Craft 3.7.37 with Commerce 3.4.12. The <code>/store/cart</code> page has a <code>&lt;form&gt;</code> with the line items in the cart next to an <code>&lt;input /&gt;</code> to change quantity and one to remove the line item. I also have an &quot;Update Cart&quot; button that updates quantities from the form (redirecting back to <code>/store/cart</code>) and a &quot;Checkout&quot; button that currently just submits the form. I've added a <a href="https://craftcms.com/docs/3.x/dev/functions.html#redirectinput" rel="nofollow noreferrer"><code>redirectInput</code></a> function to the form to redirect back to <code>/store/cart</code> on submit, which works for the &quot;Update Cart&quot; and &quot;Remove&quot; buttons.</p>
  <p>How do I make the &quot;Checkout&quot; button redirect to <code>/store/checkout/contact</code> and update cart quantities when the user clicks it?</p>
  <p>I know this is possible in PHP server side but I'm not great at PHP. I'm hoping there's a a way to use Craft's built-in functions. I want to avoid using jQuery ajax and Javascript form processing if possible (as in this <a href="https://craftcms.stackexchange.com/questions/15627/craft-commerce-add-to-cart-and-then-redirect-to-product-page">question</a>). I also want to ensure <a href="https://craftcms.stackexchange.com/questions/28794/craft-3-registration-form-success-redirect">urls are hashed</a>.</p>
  <p>Here's the relevant template code:</p>
  <pre><code>&lt;form method=&quot;POST&quot; data-cart&gt;
  &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;commerce/cart/update-cart&quot; /&gt;
  {{ redirectInput('/store/cart') }}
  {{ csrfInput() }}
  &lt;input type=&quot;hidden&quot; name=&quot;gatewayId&quot; value=&quot;{{ gatewayId }}&quot; /&gt;
  {% set item = item | default(false) %}
  {% set maxQty = maxQty | default('') %}
  {% set summary = summary | default(false) %}
  {% if item %}
  {% set sum = item.price * item.qty %}
  {% if summary %}
  &lt;p class=&quot;cart-items__item__summary&quot;&gt;
      {{ item.qty }} &amp;times; {{ item.price | currency }} = {{ sum | currency }}
  &lt;/p&gt;
  {% else %}
  &lt;div class=&quot;cart-items__item__fields&quot;&gt;
      &lt;label class=&quot;cart-items__item__qty&quot;&gt;
          {{ item.price | currency }}
          &amp;times;
          &lt;input max=&quot;{{ maxQty }}&quot; min=&quot;0&quot; name=&quot;lineItems[{{ item.id }}][qty]&quot; placeholder=&quot;qty&quot; step=&quot;1&quot; type=&quot;number&quot; value=&quot;{{ item.qty }}&quot; /&gt;
          =
          {{ sum | currency }}
      &lt;/label&gt;
      &lt;label class=&quot;cart-items__item__remove&quot;&gt;
          &lt;input name=&quot;lineItems[{{ item.id }}][remove]&quot; type=&quot;submit&quot; value=&quot;{{ item.qty }}&quot; /&gt;
          Remove
      &lt;/label&gt;
  &lt;/div&gt;
  {% endif %}
  {% endif %}
  &lt;div class=&quot;actions&quot;&gt;
      &lt;a href=&quot;/store&quot; class=&quot;button actions__action&quot;&gt;&amp;larr; Back to Store&lt;/a&gt;
      &lt;button type=&quot;submit&quot; class=&quot;button actions__action&quot;&gt;Update Cart&lt;/button&gt;
      &lt;button type=&quot;submit&quot; class=&quot;button actions__action&quot;&gt;Checkout &amp;rarr;&lt;/button&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>and a screenshot of the front-end to help clarify:
  <a href="https://i.sstatic.net/84vz7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/84vz7.png" alt="enter image description here" /></a></p>
  
Tags: <craft3><templating><php><plugin-craftcommerce3>
Title: Craft Commerce: Multiple redirect submit buttons from cart form (checkout/update cart) without ajax
ThreadText: # Craft Commerce: Multiple redirect submit buttons from cart form (checkout/update cart) without ajax
  
  (Asked by: username_0 on 2022-03-17)
  
  I'm building a basic cart page in Craft 3.7.37 with Commerce 3.4.12. The `/store/cart` page has a `<form>` with the line items in the cart next to an `<input />` to change quantity and one to remove the line item. I also have an "Update Cart" button that updates quantities from the form (redirecting back to `/store/cart`) and a "Checkout" button that currently just submits the form. I've added a [`redirectInput`](https://craftcms.com/docs/3.x/dev/functions.html#redirectinput) function to the form to redirect back to `/store/cart` on submit, which works for the "Update Cart" and "Remove" buttons.
  
  How do I make the "Checkout" button redirect to `/store/checkout/contact` and update cart quantities when the user clicks it?
  
  I know this is possible in PHP server side but I'm not great at PHP. I'm hoping there's a a way to use Craft's built-in functions. I want to avoid using jQuery ajax and Javascript form processing if possible (as in this [question](https://craftcms.stackexchange.com/questions/15627/craft-commerce-add-to-cart-and-then-redirect-to-product-page)). I also want to ensure [urls are hashed](https://craftcms.stackexchange.com/questions/28794/craft-3-registration-form-success-redirect).
  
  Here's the relevant template code:
  
  ```
  <form method="POST" data-cart>
  <input type="hidden" name="action" value="commerce/cart/update-cart" />
  {{ redirectInput('/store/cart') }}
  {{ csrfInput() }}
  <input type="hidden" name="gatewayId" value="{{ gatewayId }}" />
  {% set item = item | default(false) %}
  {% set maxQty = maxQty | default('') %}
  {% set summary = summary | default(false) %}
  {% if item %}
  {% set sum = item.price * item.qty %}
  {% if summary %}
  <p class="cart-items__item__summary">
      {{ item.qty }} &times; {{ item.price | currency }} = {{ sum | currency }}
  </p>
  {% else %}
  <div class="cart-items__item__fields">
      <label class="cart-items__item__qty">
          {{ item.price | currency }}
          &times;
          <input max="{{ maxQty }}" min="0" name="lineItems[{{ item.id }}][qty]" placeholder="qty" step="1" type="number" value="{{ item.qty }}" />
          =
          {{ sum | currency }}
      </label>
      <label class="cart-items__item__remove">
          <input name="lineItems[{{ item.id }}][remove]" type="submit" value="{{ item.qty }}" />
          Remove
      </label>
  </div>
  {% endif %}
  {% endif %}
  <div class="actions">
      <a href="/store" class="button actions__action">&larr; Back to Store</a>
      <button type="submit" class="button actions__action">Update Cart</button>
      <button type="submit" class="button actions__action">Checkout &rarr;</button>
  </div>
  
  ```
  
  and a screenshot of the front-end to help clarify:
  [![enter image description here](https://i.sstatic.net/84vz7.png)](https://i.sstatic.net/84vz7.png)
  
  **Tags:** craft3, templating, php, plugin-craftcommerce3
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-03-17. Score: 3 | Accepted answer
  
  You may be able to use the [browser-native `<button type="submit">` feature](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button)!
  
  Check this out:
  
  ```
  <form method="POST">
    {{ csrfInput() }}
    {{ actionInput('commerce/carts/update-cart') }}
  
    {# ...your existing form controls... #}
  
    <button
      type="submit"
      name="redirect"
      value="{{ 'update/path/target' | hash  }}">Update Quantities</button>
  
    <button
      type="submit"
      name="redirect"
      value="{{ 'checkout/path/target' | hash  }}">Checkout</button>
  </form>
  
  ```
  
  The only peculiar thing to note here is the direct use of Craft's [`hash` filter](https://craftcms.com/docs/3.x/dev/filters.html#hash)—ultimately the same functionality that `redirectInput()` helper provides, as a means to prevent tampering with the values and exposing your app to remote code execution vulnerabilities!
  
  > **Comments:**
  > **username_0**: Thanks! This works like a charm. For anyone else wondering the `hash` filter is necessary. If you don't do it you'll get `HTTP 400 – Bad Request`.  
  
  ---
  
  