Forum

Thread tagged as: Question, Forms

Passing Variable through to a Form template

Hello,

I've got a contact form which is being pulled in using:

<?php perch_form('general_contact_form.html'); ?>

The form is basic and has the usual Name, Email and Message fields. I've a specific page I'm setting a cookie on, so If someone visits the brighton.php page a cookie is set with the name of 'location' and a value of 'brighton'. If someone submits the contact form on a separate page, and they have this cookie set, I'd like this value (brighton) to passed through to the general_contact_form.html template where I can then set it as a hidden input field on the contact page so it can be sent to my client.

What is the best way of doing this? I'm wondering if this needs setting at a runtime level rather than passing through with the perch_form tag?

Thanks.

Joe Proctor

Joe Proctor 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you tried with PerchSystem::set_var() ?

Yes I've got so far, seems like I'm just missing the final piece.

So at the top of the contact.php page I'm declaring:

<?php PerchSystem::set_var('location','Edinburgh'); ?>

Then calling the form template on the contact.php page:

<?php perch_form('general_contact_form.html'); ?>

Within the template I'm trying to access the variable like so:

<p>location = <perch:content id="location"/></p>

However this doesn't show anything (apart from the "location =").

If I do <perch:showall /> it does indeed show the ID / Variable is there:

ID Value location Edinburgh perch_page_path /contact.php perch_namespace perch:forms

So I assume I'm doing something wrong with this part:

<perch:content id="location"/>

I've managed to get it working, though looks like it's only accessible as an input type.

<perch:input type="hidden" id="location" name="location" label="location" />

So I'm unable to output the variable value, but can access it and pass it through as an input where the value is the variable.

I could be wrong, but have you tried by calling the form using perch_content_custom? I am sure I've done something in the past with this, where I had to just call the form in slightly differently. I'll see if I can dig it out and see what I actually did.