Forum
Auto Populate Form Input From Referring Page
I would like to populate an input on a form (which uses the form app) depending on the page that the user has navigated from.
i.e. If if they click a link on a product page to ‘contact us’ the product name will be used to populate the input.
I am using product detail/listing and the contact form is on a contact page.
Here’s the detail template:
<div class="row product-detail">
<div class="medium-6 columns">
<perch:if exists="image">
<perch:content id="image" type="image" label="Image" width="640" output="tag" />
<perch:else />
<img src="https://placehold.it/640/d7c5b4/ddd&text=Product Image" alt="" />
</perch:if>
</div>
<div class="small-12 medium-6 columns">
<h2>
<perch:content id="title" type="text" label="Product Name" required="true" title="true" />
</h2>
<perch:content id="desc" type="textarea" label="Description" textile="true" editor="markitup" />
<perch:content id="slug" for="title" type="slug" suppress="true" />
<hr>
<p><strong>Want this product?</strong></p>
<a href="/contact?<perch:content id="title" />" class="btn">Contact Us</a>
<a href="/products" class="more">← Back to products</a>
</div>
</div>
Here’s the products.php page
<?php
include('perch/runtime.php');
perch_layout('header', array(
'title' => 'Edelweiss Antiques Products',
'heading' => 'Products',
));
perch_content_create('Products', array(
'template' => 'product_detail.html',
'multiple' => true,
'edit-mode' => 'listdetail',
));
if (perch_get('s')) {
perch_content_custom('Products', array(
'template' => 'product_detail.html',
'filter' => 'slug',
'match' => 'eq',
'value' => perch_get('s'),
'count' => 1,
));
} else {
perch_content_custom('Products', array(
'template' => 'product_listing.html',
));
}
perch_layout('footer');
?>
The contact form is included on a contact.php page using:
<?php perch_form('contact.html') ?>
I have tried using PerchSystem::set_var('name', 'value’) to pass a variable in the contact.html template but no joy so far.
Is this possible?
Thanks in advance
What did you try in your form template?
Well I only got as far as trying to output the variable with:
On my products page
In my form template
If you're using
perch_form()
to display the form,<perch:content />
tags won't get processed.I think you might be able to use
<perch:forms />
I changed the form template to use:
but it still doesn't output anything.
Does
<perch:showall />
reveal anything?Yes:
So the
test
value isn't there.Can you show us your page code in context?
I use PerchSystem::set_var('test', 'value'); if the page to be displayed is the detail view:
Then I try and render the 'test' variable at the bottom of my contact.html template:
Where on that page is
contact.html
being used?On the detail page there is a link to the contact page. I'm now realising that PerchSystem::set_var can't be used to pass variables between pages.
Can you use perch:layout as the value of a perch:input?
eg:
Or I guess I can pass the name of the product via a query string and then use js to populate the value of the input from that query string.
Using the query string is a valid approach to passing data from one page to the next.
You don't then need to use JavaScript - you can still use PHP.
How though, if the form template is html?
The calling context is PHP - so you'd pass the variable into the template as demonstrated.
Okay so I got it to work by doing:
Cheers drew.
Great!