We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Forms

Is it possible to mix perch:content with perch:form?

Hi,

I've made a simple jobs listing section, using the How do I create list and detail pages in Perch solution as a starting point.

One of the Perch fields on the jobs_detail.html template is <perch:content id="job-reference" />, it'd be neat if I could pull that into the form (in the same template). So when the form is submitted the job reference is sent with it.

Is this possible?

Stephen Meehan

Stephen Meehan 4 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, if you display the form using either perch_content() or perch_content_custom()

Ah, great that it's possible!

I can see in the perch_content_custom documentation, under the section Using content from different pages shows this can be done (brilliant).

Question:

I've taken the code example from the documentation as a starting point:

perch_content_custom('Properties', array(
    'page'=>'/properties.php'
));

I want to pull <perch:content id="ref-code"/> out of the jobs_detail.html template, which is being used on the work-with-us page. work-with-us uses a template called jobs.php. I've put the code below on jobs.php.

perch_content_custom('ref-code', array(
    'page'=>'work-with-us/*'
        'template' => 'forms/job_form.html',
));

I've used the * (wildcard) in 'page' because as I mentioned in my first post I\m using the How do I create list and detail pages in Perch solution as a starting point.

At the moment this isn't working....

Here's the PHP i'm using on jobs.php

<?php

     perch_content_create('Jobs', array(
          'template'  => 'jobs/jobs_detail.html',
          'multiple'  => true,
          'edit-mode' => 'listdetail',

     ));


     if (perch_get('s')) {

          // Detail mode
          perch_content_custom('Jobs', array(
               'template' => 'jobs/jobs_detail.html',
               'filter'   => 'slug',
               'match'    => 'eq',
               'value'    => perch_get('s'),
               'count'    => 1,
          )); 
          // Is it ok to use two perch_content_custom?
          perch_content_custom('ref-code', array(
               'template' => 'forms/job_form.html',
               'page'=>'/work-with-us/*'
          ));

     } else {

          // List mode
          perch_content_custom('Jobs', array(
               'template' => 'jobs/jobs_listing.html',
               'sort' => 'job-date-posted',
               'sort-order' => 'DESC',
          )); 
     }



?>

Hope you can help.

Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't quite follow what you're hoping will happen with those two perch_content_custom() calls. Is the intention that the data will get passed from one to the other?

The PHP uses a if / else statement, so I figured if I wanted the form to appear on the job page I had to put (the code below) within the if statement

perch_content_custom('ref-code', array(
    'page'=>'work-with-us/*'
        'template' => 'forms/job_form.html',
));

What I want to happen is:

To have the form appear beneath the job (on the detail page, not the listing page) and pass the <perch:content id="ref-code" /> to the form. when the form is submitted I'd like to include the <perch:content id="ref-code" /> with it.

I realise the current code doesn't even begin to do that bit, but I was hoping to at least get the form working before tackling that bit :)

Hi Drew

I've managed to get this to half work.

I used another perch_content_create to create a new region called Job-Form. This uses the form template I want to appear under each job.

I then used perch_content('Job-Form'); within the if statement to pull in the Perch region only when the page is in Detail Mode

I'm rather happy!

The last bit of the puzzle (which I hope you can help with) is:

How do I pass <perch:content id="job-reference" /> from jobs_detail.html to the form?

<?php

     perch_content_create('Jobs', array(
          'template'  => 'jobs/jobs_detail.html',
          'multiple'  => true,
          'edit-mode' => 'listdetail',

     ));
      // This creates the job form region
     perch_content_create('Job-Form', array(
          'template'  => 'forms/job_form.html',


     ));


     if (perch_get('s')) {

          // Detail mode
          perch_content_custom('Jobs', array(
               'template' => 'jobs/jobs_detail.html',
               'filter'   => 'slug',
               'match'    => 'eq',
               'value'    => perch_get('s'),
               'count'    => 1,
          )); 
        // This displays the job form region, only shows at the bottom of the jobs_detail page.
            perch_content('Job-Form');




     } else {
            perch_content('Main');
          // List mode
          perch_content_custom('Jobs', array(
               'template' => 'jobs/jobs_listing.html',
               'sort' => 'job-date-posted',
               'sort-order' => 'DESC',
          )); 
     }



?>

Drew McLellan

Drew McLellan 2638 points
Perch Support

Try:

// Detail mode
$result = perch_content_custom('Jobs', array(
    'template' => 'jobs/jobs_detail.html',
    'filter'   => 'slug',
    'match'    => 'eq',
    'value'    => perch_get('s'),
    'count'    => 1,
    'skip-template' => true,
    'return-html' => true;
)); 

echo $result['html'];

// This displays the job form region, only shows at the bottom of the jobs_detail page.
PerchSystem::set_vars($result[0]);
perch_content_custom('Job-Form');

Thanks for replying so quick (and on a Sunday!)

Unfortunately that code doesn't appear to work. The page doesn't render any HTML.

Maybe I've implemented it wrong?

<?php

     perch_content_create('Jobs', array(
          'template'  => 'jobs/jobs_detail.html',
          'multiple'  => true,
          'edit-mode' => 'listdetail',

     ));
      // This creates the job form region
     perch_content_create('Job-Form', array(
          'template'  => 'forms/job_form.html',


     ));


     if (perch_get('s')) {

    // Detail mode
    $result = perch_content_custom('Jobs', array(
    'template' => 'jobs/jobs_detail.html',
    'filter'   => 'slug',
    'match'    => 'eq',
    'value'    => perch_get('s'),
    'count'    => 1,
    'skip-template' => true,
    'return-html' => true;
    )); 

    echo $result['html'];

    // This displays the job form region, only shows at the bottom of the jobs_detail page.
    PerchSystem::set_vars($result[0]);
    perch_content_custom('Job-Form');




     } else {
            perch_content('Main');
          // List mode
          perch_content_custom('Jobs', array(
               'template' => 'jobs/jobs_listing.html',
               'sort' => 'job-date-posted',
               'sort-order' => 'DESC',
          )); 
     }



?>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you get any errors on the page or in your server's error log?

Page was blank but the php_error.log helped identify (part of) the problem.

PHP Parse error:  syntax error, unexpected ';', expecting ')' in /Users/stephenmeehan/Repositories/project-name/perch/templates/pages/jobs.php on line 48

I used the error log to locate and remove the semicolon here:

//deleted the semicolon
'return-html' => true;

The page works now and the form displays on the page under the job, but it's not pulling in the <perch:content id="ref-code" /> from jobs_detail.html into the form.

How would I do that?

Drew McLellan

Drew McLellan 2638 points
Perch Support

What's the ID from the content region that you're trying to use?

Hi Drew

The content region ID is called Jobs.

I would like to be able to use <perch:content id="ref-code" /> from content region ID Jobs and insert it into content region ID Job-Form

The idea being (hopefully) when the form is submitted the unique ref-code is sent along with it.

Drew McLellan

Drew McLellan 2638 points
Perch Support

So the ID is ref-code in both? (which is an invalid ID, btw)

Hi Drew

Yes. <perch:content id="ref-code" /> is set (and used) on the jobs_detail.html template. It's also used on the jobs_listing.html template to display the unique job reference code.

When a job is selected from the jobs_listing page, the jobs_detail page is shown. At the bottom of the page is a form. I'd like to be able to re-use the <perch:content id="ref-code" /> in the form (if possible).

Why is ref-code an invalid ID? It seems to be working fine. Is it a system only ID? Or is it the hyphen?

Thanks for taking the time to reply!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Hyphens aren't legal in IDs. It may work, but I can't guarantee it continuing to work.

Does <perch:input type="hidden" id="ref-code" /> work?

.....

It works! :)

I was sure I tried that right at the start of all of this, before posting to the forums.

It must of been the hyphen...

Thank you so much for your help (and patience).

I'm off to Twitter now to write a glowing tweet about the phenomenal support you guys give.