Perch Forms: Using the same forms on multiple pages but passing a value from the page into a hidden field

  • I have a perch form that I want to include on several pages.


    The reason I'm including it on multiple pages is I want to use exactly the same form fields.. except one.


    I have a field called My Closest Pharmacy. There are seven options in this select dropdown.


    Depending upon what page you're on.. I would like this field to be pre populated. Either with the correct option for that page selected. Or even as a hidden field where the value is passed through to the form submission in the background.

    So if for example.. If I am on the Newport Pharmacy page. I want the value 'Newport' to be passed into this field. If I'm on the Cardiff page then the value needs to be 'Cardiff'


    I don't want the user to have to manually select the Pharmacy themself.

    What is the best way to do this using Perch? Or would I need to create Several almost identical forms just to have this one different field?

  • You can. Have a look at https://docs.grabaperch.com/te…variables-into-templates/


    You set the system variable before you call your template in the php file

    PHP
    1. <?php PerchSystem::set_var('id_name', 'value'); ?>


    Then in your html template that becomes available as:

    Code
    1. <perch:content id="id_name" />

    You don't have to use id_name, it can be anything. eg.

    PHP
    1. <?php PerchSystem::set_var('closest_pharmacy', 'Birmingham'); ?>

    To perform something conditional use a perch:if statement in your html file:

    Code
    1. <perch:if id="closest_pharmacy" value="Birmingham" match="eq"/>
    2. Birmingham is the closest pharmacy.
    3. <perch:if>

    More about perch if can be found here: https://docs.grabaperch.com/templates/conditionals/if/


    Hope this helps.

  • Thanks for the speedy repsonse.

    So this works with perch:form aswell?

    So on my page I could have:


    PHP
    1. <?php PerchSystem::set_var('pharmacyName', 'Birmingham'); ?>
    2. <?php perch_content('Pharmacy Contact Form'); ?>

    and then in my form template:


    Code
    1. <perch:input type="hidden" id="pharmacyName" label="Closest Pharmacy">


    And that would pass that value into the hidden form field so that it passes that value to the output of my form.

    So:

    Closest Pharmacy: Birmingham


    Is that correct?

  • One other question.

    If wanted to create a page template and I wanted to pass through the 'Page Title' that's created in Perch Admin as the variable.

    Is that possible. e.g.

    I create a page using a page template called 'Birmingham'


    The page template contains the following:


    PHP
    1. <?php PerchSystem::set_var('pharmacyName', 'Page Title'); ?>
    2. <?php perch_content('Pharmacy Contact Form'); ?>


    So that the value that gets set is the Page Title.. in this case 'Birmingham'

  • Yes, try <?php perch_pages_title(); ?>,


    PHP
    1. <?php $pageTitle = perch_pages_title(true); ?>
    2. <?php PerchSystem::set_var('pharmacyName', $pageTitle); ?>

    The true part in the brackets stops perch echoing the title onto the page. Then put your variable in where the value normally goes (no need for single quotes).


    https://docs.grabaperch.com/fu…gation/perch-pages-title/


    It's always useful to have the page title available.

  • It is not possible to pass variables to the template when using perch_content(). Use perch_content_custom() instead:

    PHP
    1. PerchSystem::set_var('pharmacyName', 'Page Title');
    2. perch_content_custom('Pharmacy Contact Form');



    You can use the data option if you want the variable to only be available to this region:

    PHP
    1. // can access pharmacyName
    2. perch_content_custom('Pharmacy Contact Form', [
    3. 'data' => [
    4. 'pharmacyName' => 'Page Title',
    5. ],
    6. ]);
    7. // cannot access pharmacyName
    8. perch_content_custom('Another Region');
  • Ok so I've got one where I'm trying to pass a type of condition through to a form.

    In my conditions.php master page template I have:


    PHP
    1. <?php PerchSystem::set_var('condition', $pageTitle); ?>
    2. <?php perch_content('Common Ailments Form'); ?>


    Then on the Common Ailments Form I have

    Code
    1. <perch:input type="hidden" id="condition" label="Condition" />


    I have setup a page with the Page title 'Acne'


    So it should be passing Condition: Acne in the form submission.

    Instead I'm getting an empty field:


    Any ideas?