Forum

Thread tagged as: Question, Problem

Pull content id into a hidden form field

Hi,

Is there anyway to pull the value from a content field in one region, to a hidden field in a form which lives in a seperate region? I know if they were in the same template for a region that I could do it, but what if they are seperate?

For instance:

training-camp.html

<perch:content id="trainingCampTitle" type="text" />

training-camp-form.html

<perch:form id="trainingCampForm" class="is-Form" method="post" prefix="form" app="perch_forms" role="form">
 <perch:input type="hidden" id="ref" value="<perch:content id="trainingCampName" type="text"/>" />
</perch:form>
Mathew Doidge

Mathew Doidge 2 points

  • 5 years ago

Hi Mat,

In your page's .php you can display the form with perch_content_custom() and pass the first region as the first argument.

So if the name of your first region was * Training Camp Title* you would do

perch_content_custom('Training Camp Title', array(
            'template'=>'training-camp-form.html',
        ));

https://docs.grabaperch.com/functions/content/perch-content-custom/

I hope this helps.

Hi Prokopios,

That didn't seem to do it for me. I did managed to sort of achieve what I was after, although it had issues.

So here my template training-camps.php

<?php 
    perch_content_create('Camp Details', array(
        'template'=>'camp-details.html',
        'multiple' => false,
    )); 

    perch_content_create('Simple Paymentform', array(
        'template'=>'single-Paymentform.html',
        'multiple' => false,
    )); 
?>

<?php 
    perch_content_custom("Camp Details", array(
        'template'=>'camp-details.html',
    )); 
?>

<?php
    perch_content_custom(array('Simple Paymentform','Camp Details'), array(
        'template'=>'single-Paymentform.html',
    ));
?>

single-Paymentform.html

<perch:input type="hidden" id="ref" value="<perch:content id="campName" type="text" type="hidden" />" />

So I create my two regions, then call them in using perch_content_custom() - the problem is that I am getting two forms appearing. But I am getting the camp name placed into my hidden form field. I just can't work out why I am getting two forms - once I enter the thank you text into the editable field for the form region. It's at the point of hitting enter and submitting the data that I get a second form appearing.

I found the method for it here: https://docs.grabaperch.com/perch/content/functions/content-from-multiple-pages-and-regions/

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'll need to get the value and pass it into the form template with PerchSystem::set_var().

https://docs.grabaperch.com/templates/passing-variables-into-templates/

Drew McLellan said:

You'll need to get the value and pass it into the form template with PerchSystem::set_var().

https://docs.grabaperch.com/templates/passing-variables-into-templates/

Hi Drew,

Thanks! I have managed to pull out an array of the contents from that region using:

$camptitle = perch_content_custom('Camp Details', array(  
    'template' => 'camp-details.html', 
    'skip-template' => true,
    'return-html' => false,
));  

PerchSystem::set_var('camp_title', $camptitle);

I'm stuck trying to work out how I can then access one of the individual IDs from that array. The output of that system var is:

[camp_title] => Array ( [0] => Array ( [_id] => 23 [campName] => A Test Camp [campDate] => 2016-10-09 [campCost] => [_page] => /training-camps.php
Drew McLellan

Drew McLellan 2638 points
Perch Support

Which property do you want?

Trying to access campName

Simon Clay

Simon Clay 127 points

You'd use: $camptitle[0]['campName']; to call the ID from the array. e.g.

PerchSystem::set_var('camp_title', $camptitle[0]['campName']);

Thank you Simon and Drew. Working perfectly.