Forum
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, 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 useperch_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 usingskip-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 templateStep 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):/templates/events/event-detail.html
(event template):/templates/content/event-detail.html
(form template):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...
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...
Thanks, Robert!
To clarify,
set_vars($event[0]);
would replace which part of the code above? Both of these…… 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.
all of these will already be set if...
PerchSystem::set_vars($event[0]);
... so here's my 2 cents worth...
Then in your html template you could test...
Does this make sense?