perch:if does not work inside perch:form tag

  • Hello,

    I'm trying to insert a next attribute into a form to redirect customers to different places depending on the stage in the registration process. However no method I try is inserting the next attribute dynamically:

    Set the status as a system variable depending:

    1. $status = perch_get('status'); /*Set to 'checkout' */
    2. PerchSystem::set_var('status', $status);

    Check for the value of the status:

    1. <perch:form id="register" class="customerDetails" method="post" app="perch_shop" <perch:if id="status" value="checkout" match="eq">next="/shop/checkout"</perch:if>>
    2. <h4>New Customers</h4>
    3. <perch:if id="status" value="checkout" match="eq">Redirect back to checkout</perch:if><!--This shows the code-->

    The if statement works in the body of the form, but not in the opening tag. It does not redirect back to the checkout.

    I've tried setting the redirect as a variable and passing it in:

    1. <?php
    2. if($status == 'checkout'){ $redirect = 'next="/shop/checkout"';} else {$redirect = 'next="/shop/account"';}
    3. PerchSystem::set_var('redirect', $redirect);
    4. ?>
    1. <perch:form id="register" class="customerDetails" method="post" app="perch_shop" <perch:shop id="redirect">

    This does not work either.

    Only hard coding the next attribute into the template works, but this means I have to maintain separate templates for all of the redirect scenarios.

  • Hi Tony,

    The next attribute is not a valid attribute for <form> in HTML5. Perch only keeps valid attributes and strips the rest.

    The next attribute is actually a Perch tag attribute (like app) that gets stripped when the form is rendered. Some apps like Perch Members and Perch Shop use the next attribute to redirect upon a successful submission. The next attribute cannot have a dynamic value.

  • Nevermind, in this instance keeping multiple forms will be a frustration but manageable.

    You don't need multiple forms. You can add a hidden field to the form:

    1. <perch:input id="r" type="hidden">

    You can dynamically populate this field in 2 ways:

    (1) Add a URL query parameter to your URL /account?r=/checkout

    (2) OR pass a variable in PHP to the template engine:

    1. PerchSystem::set_var('r', '/checkout');

    And at the top of the page you can redirect:

    1. if(perch_member_logged_in() && perch_post('cms-form') && perch_post('r')) {
    2. PerchSystem::redirect(perch_post('r'));
    3. }