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…variables-into-templates/

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

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

    Then in your html template that becomes available as:

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

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

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

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

    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:

    Hope this helps.

  • Thanks for the speedy repsonse.

    So this works with perch:form aswell?

    So on my page I could have:

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

    and then in my form template:

    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.


    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:

    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(); ?>,

    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).…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:

    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:

    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:

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

    Then on the Common Ailments Form I have

    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?

  • Following on from my previous post. Was wondering if I could get some help.

    I have a page called job.php which contains the following:

    1. <?php
    2.     perch_content_custom('Jobs', array(
    3.         'page' => '/opportunities/index.php',
    4.         'template' => 'opportunities/job_detail.html',
    5.         'filter' => 'slug',
    6.         'match' => 'eq',
    7.         'value' => perch_get('s'),
    8.         'count' => 1,
    9.     ));
    10. ?>

    In my job_detail.html template I have a perch content area for Job Title:

    1. <perch:content id="jobTitle" type="text" label="Job Title" required="true" title="true" />

    I have a form with the template: apply_for_job.html that contains: (I used different ID's to avoid confusion)

    1. <perch:input type="hidden" id="jobFormTitle" label="Job Title" />

    I would like to pass the perch:content id="jobTitle" from my job_detail.html template into my apply_for_job.html (form template) via the job.php page.

    From the discussion above I know I need to have the following:

    1. <?php $jobTitle = 'Job Title Goes Here'(true); ?>
    2. <?php PerchSystem::set_var('jobFormTitle', $jobTitle); ?>
    3. <?php perch_content('Apply For Job Form'); ?>

    The problem is I can't work out what I need to put in the 'Job Title Hoes Here' bit..

    Can anyone help?

  • Do you need a separate region for the form? If you are not adding any editable content to the form template, you don't need a new region; instead you can add the form to your job_detail.html template (where you already have access to the job title):

    1. <perch:input type="hidden" id="jobFormTitle" label="Job Title" value="<perch:content id="jobTitle">" />

    If you need a region, then you can the skip templating when fetching the job, get the title and pass it as a variable:

  • I would like to do the same with an event. I tried using the code above but modified it for events but the Event title isn't coming through.

    So in my event.php file I have

    In my listing/event-detail.html I have:

    1. <h1 class="summary margin-bottom-20"><perch:events id="eventTitle" /></h1>

    and in my forms/register-your-interest-to-an-event.html I have

    1. <perch:input type="hidden" id="eventFormTitle" label="Event Title" />

    I thought the same code would work.. Just wondering if because maybe it's using the event app that it may need to be slightly different?

    Any help would be very much appreciated.

  • If you're following the same approach, yYou need to use the skip-template and return-html options. I don't know how well supported they are in the Events app (it is an old app):

    1. $result = perch_events_custom(array(
    2. 'filter' => 'eventSlug',
    3. 'match' => 'eq',
    4. 'value' => perch_get('s'),
    5. 'template' => 'listing/event-detail.html',
    6. 'skip-template' => true,
    7. 'return-html' => true,
    8. ));