Forum

Thread tagged as: Question, Forms

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>
    &nbsp;
    <a href="/products" class="more">&larr; 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

Creative Monster

Creative Monster 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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

PerchSystem::set_var('test', 'value')

In my form template

<perch:content id="test" />
Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

<perch:forms id="test" />

but it still doesn't output anything.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Does <perch:showall /> reveal anything?

Yes:

ID  Value
perch_page_path /contact.php
perch_namespace perch:forms
Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

<?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')) {

    PerchSystem::set_var('test', 'value');

    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');

?>

Then I try and render the 'test' variable at the bottom of my contact.html template:

<perch:form id="contact" method="javascript:void(0);" app="perch_forms">
    <div class="form-name">
        <perch:label for="name">Name</perch:label>
        <perch:input type="text" id="name" required="true" label="Name" />
        <p><perch:error for="name" type="required">Please add your name</perch:error></p>
    </div>

    <div class="form-email">
        <perch:label for="email">Email</perch:label>
        <perch:input type="email" id="email" required="true" label="Email" placeholder="you@company.com" />
        <p><perch:error for="email" type="required">Please add your email address</perch:error></p>
        <p><perch:error for="email" type="format">Please check your email address</perch:error></p>
    </div>

    <div class="">
        <perch:label for="tel">Phone Number</perch:label>
        <perch:input type="text" id="tel" required="false" label="Telephone" />
        <br>
        <br>
    </div>

    <div class="form-message">
        <perch:label for="message">Message</perch:label>
        <perch:input type="textarea" id="message" required="true" label="Message" rows="6" />
        <p><perch:error for="message" type="required">Please add a message</perch:error></p>
    </div>

    <div id="hp" class="hidden">
    <perch:label for="hp">Leave this field blank</perch:label>
    <perch:input type="text" id="hp" antispam="honeypot" />
  </div>

    <div class="submit-form">
        <perch:input type="submit" id="submit" value="Send" class="btn" class="btn"/>
        <br>
        <br>
    </div>

    <div class="clearfix"></div>

    <p><perch:forms id="test"/></p>

  <perch:success>
        <script>
                return 'SUCCESS';
        </script>
  </perch:success>
</perch:form>

Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

<perch:input type="text" id="name" required="true" label="Name" value="<perch:layout path='test' "/>

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

<perch:input type="text" id="product" required="false" label="Product" value="<perch:forms id="prod" />" />

Cheers drew.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Great!