Forum

Thread tagged as: Question, Events, Forms

Fill forms with event data (problem with namespace/paths?)

Hi everyone,

I have an event page that displays event details. On that page is a perch form that acts as a sign-up form. I want to add hidden fields to the form that store the event details and transfer them to Perch and ultimately email them to the user.

According to the docs, form templates have to be either in templates/content/ (if they need editable content). Or you can use perch_form() when you don’t need editable content (I don’t), in which case my template has to be in templates/forms/.

How can I put event values into the form template and transfer them into Perch and my email?

My form is processed correctly with the perch_content()or perch_form() method, but my <perch:event …> tags are completely ignored, probably due to the namespace and template paths?

templates/pages/events/event-detail.php:

…
    <?php
    perch_events_custom(array(
        'filter' => 'eventSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'template' => 'listing/event-summary.html'
    ));
    ?>

…

    <?php perch_form('form--event.html'); ?>
…

templates/events/listing/event-summary.html:

…
        <h3>Termin:</h3>
        <p><perch:events id="eventDateTime" type="date" format="%A, %e. %B %Y" time="true" label="Datum" /></p>

        <h3>Ort:</h3>
        <p><perch:events id="eventLocation" /></p>

        <h3>Preis:</h3>
        <p>
            <perch:if exists="eventPriceVKK">
                <strong>Mitglieder: <perch:events id="eventPriceMembers" /></strong> <br>
                Nichtmitglieder: 
            </perch:if>
                <perch:events id="eventPrice" />
        </p>
…

templates/forms/form--event.html:

…
      <perch:input type="hidden" value="Weiterbildungsort: <perch:event id="eventLocation" />" />
      <perch:input type="hidden" value="Erster Termin: <perch:event id="eventDateTime" />" />
      <perch:input type="hidden" value="Preis: <perch:event id="eventPriceMembers" /> für Mitglieder, <perch:event id="eventPrice" /> für Nichtmitglieder" />
…
Jannis Borgers

Jannis Borgers 0 points

  • 5 years ago

Jannis, this post may help.

https://forum.grabaperch.com/forum/11-16-2015-adding-a-form-and-passing-string-to-hidden-field-on-events-page

The only thing you may need different is passing variables into the template but let's see if the referenced thread helps first.

Thank you, Robert! This helped, although I found some hurdles on the way.

I was visiting the Perch slack channel today and Dexter Harrison was nice enough to spend quite some time explaining to me how this has to be set up. So for everyone else, this is the way it works:

Step 1: Use perch_content_create to create your form (you can’t use perch_content because of the way steps 5 and 6 work)

Step 2: Add and display your event region with perch_events_custom

Step 3: Use perch_events_custom again, this time storing it in an array using skip-template

Step 4: Access the events-array and store what you need in variables

Step 5: Turn your variables into Perch variables using PerchSystem::set_vars() to pass them into the form template

Step 6: Use `perch_content_custom' to display your form, which now has access to the variables, and thus to your event data

The variables you retrieved from the event are passed into the form template, where you can output them as values in hidden input fields, which will be submitted to Perch or e-mail.

/templates/pages/events/event-detail.php (event page):

<?php

    // Set form up for later output
    perch_content_create('Anmeldeformular Weiterbildung', array(
        'template' => 'vkk-body--events.html'
    ));
?>

// Other stuff happening here, mark-up, other regions etc …

<?php

    // display your event details
    perch_events_custom(array(
        'filter' => 'eventSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'template' => 'event-detail.html'
    ));

    // do the same, but store it in an array instead of outputting it
    $event = perch_events_custom(array(
        'filter' => 'eventSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'skip-template' => 'true'
    ));

    // store data from your event into variables …
    $eventTitle = $event[0]['eventTitle'];
    $eventLocation = $event[0]['eventLocation'];
    $eventDateTime = $event[0]['eventDateTime'];
    $eventPrice = $event[0]['eventPrice'];
    $eventUnits = $event[0]['eventUnits'];

    // … turn the variables into variables to be accessed by Perch
    PerchSystem::set_vars(array(
        'eventTitle' => $eventTitle,
        'eventLocation' => $eventLocation,
        'eventDateTime' => $eventDateTime,
        'eventPrice' => $eventPrice,
        'eventUnits' => $eventUnits
    ));

?>

/templates/events/event-detail.html (event template):

<!-- … -->

<h3>Time of event:</h3>
<p>
    <perch:events id="eventDateTime" type="date" format="%A, %e. %B %Y" time="true" label="Date" />
</p>
<h3>Location of event:</h3>
<p>
    <perch:events id="eventLocation" type="text" label="Location" />
</p>

<!-- … -->

/templates/content/event-detail.html (form template):

<!-- … -->

<!-- hidden inputs need IDs, too! -->
<!-- the content tags have to be of type="hidden", otherwise you get empty fields in Perch -->

<perch:input id="eventLocation" label="Weiterbildungsort" type="hidden" value="<perch:content id="eventLocation" type="hidden" />" />
<perch:input id="eventDateTime" label="Erster Termin" type="hidden" value="<perch:content id="eventDateTime" type="hidden" />" />

<!-- … -->

Thank you, Dexter! This is awesome community support!

Cheers, Jannis

If you don't need/want to cherry pick the results of $event you could always...

PerchSystem::set_vars($event[0]);

and pass all the variables of the event to the template.

One thing worth mentioning: always test the return of $event to make sure there are results, otherwise you will get lots of errors when you try to reference an offset in the $event array.

One way to do this is...

if (PerchUtil::count($event)) {
    // assign variables here
}

Thanks, Robert!

To clarify, set_vars($event[0]); would replace which part of the code above? Both of these…

    $eventTitle = $event[0]['eventTitle'];
    $eventLocation = $event[0]['eventLocation'];
    $eventDateTime = $event[0]['eventDateTime'];
    $eventPrice = $event[0]['eventPrice'];
    $eventUnits = $event[0]['eventUnits'];
    PerchSystem::set_vars(array(
        'eventTitle' => $eventTitle,
        'eventLocation' => $eventLocation,
        'eventDateTime' => $eventDateTime,
        'eventPrice' => $eventPrice,
        'eventUnits' => $eventUnits
    ));

… or just the latter?

And for your second trick: Do you mean if any of the fields are empty, it will throw an error? I tried by removing the Units from one event, but the form was sent successfully.

Jannis Borgers said:

Thanks, Robert!

To clarify, set_vars($event[0]); would replace which part of the code above? Both of these…

all of these will already be set if...

   $eventTitle = $event[0]['eventTitle'];
   $eventLocation = $event[0]['eventLocation'];
   $eventDateTime = $event[0]['eventDateTime'];
   $eventPrice = $event[0]['eventPrice'];
   $eventUnits = $event[0]['eventUnits'];

PerchSystem::set_vars($event[0]);

... so here's my 2 cents worth...

    // do the same, but store it in an array instead of outputting it
    $event = perch_events_custom(array(
        'filter' => 'eventSlug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'skip-template' => 'true'
    ));

   // store data from your event into variables …
  if (PerchUtil::count($event)) {
    // assign variables here
    PerchSystem::set_vars($event[0]);
  }

Then in your html template you could test...

<perch:if exists="eventTitle">do something</perch:if>

Does this make sense?