Forum

Thread tagged as: Problem, Error

Ajax call to get content from a region

I have a few sub pages that have a content region called 'Description'. On the home page, I have a small widget that displays the those descriptions based on what the user selects. It works by just doing a post call to a php script below:

<?php include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');

  $slug = $_POST['slug'];
  echo $slug;

  $content = perch_content_custom('Description', array(
    'page' => $slug,
    'skip-template' => true,
    'return-html' => true
  ), true);

  echo var_dump($content);

?>

At the moment I am just trying log the response to the console, but I get the error:

"The template <code></code> could not be found."

which, according to this thread should be sorted out by the "Description" argument.

The desired content is just a single paragraph.

Locky Keaney

Locky Keaney 0 points

  • 4 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you reduced it to a basic test case?

perch_content_custom('Description', [
    'page' => '/path/to/your/page.php' 
]);

What result do you get?

Pretty much the same error:

The template <code></code> could not be found.NULL

The NULL appears when I use var_dump. Echoing just the variable gives the same error as before.

The same widget also gets a list of the navigation items on the page, which works fine:

  $slug = $_POST['slug'];

  $arr = perch_pages_navigation(array(
    'from-path' => $slug,
    'levels' => 0,
    'skip-template' => true,
  ), true);


  foreach($arr as $item) {
    echo '<li class="course-widget--list-item">'.'<a href="'.$item["pagePath"].'">'.$item["pageTitle"].'</a></li>';
  }

Could that be interfering?

Drew McLellan

Drew McLellan 2638 points
Perch Support

It sounds like that region doesn't exist. If you turn on debug, what does it output?

I've placed the ~~~ PerchUtil::output_debug(); ~~~ debugger in a few spots on the page, and in the script that Ajax calls, but I can't get it do to return anything at all.

This is on the bottom of the admin page with the description region:

Debug Message - Perch 3.0.6
[1] SELECT u.*, r.* FROM perch3_users u, perch3_user_roles r WHERE u.roleID=r.roleID AND u.userEnabled=1 AND u.userID=1 AND u.userHash='d787f3f3637debc33c6a411c5637252b' LIMIT 1
UPDATE perch3_users SET userHash='ea7499758dbed3f41206229310d4ecb7' WHERE userID='1'
[40] SELECT p.privKey FROM perch3_user_privileges p
[14] SELECT settingID, settingValue, userID FROM perch3_settings WHERE userID=1 OR userID=0 ORDER BY userID ASC
[8] SELECT itemValue FROM perch3_menu_items WHERE itemType='app'
[1] SELECT * FROM perch3_content_regions WHERE regionID=165 LIMIT 1
[1] SELECT * FROM perch3_pages WHERE pageID='25' LIMIT 1
Using template: /templates/content/text_block.html
[1] SELECT * FROM perch3_content_items c WHERE c.regionID=165 AND c.itemRev=1 ORDER BY c.itemOrder ASC
Using template: /templates/content/text_block.html
[1] SELECT * FROM perch3_content_items c WHERE c.regionID=165 AND c.itemRev=1 ORDER BY c.itemOrder ASC
Using template: /templates/content/text_block.html
Form not posted or did not validate
[1] SELECT * FROM perch3_menu_items WHERE itemActive=1 AND itemType='menu' AND parentID=0 ORDER BY itemOrder ASC LIMIT 0, 1
[3] SELECT mi.*, p.privKey FROM perch3_menu_items mi LEFT JOIN perch3_user_privileges p ON mi.privID=p.privID WHERE mi.itemActive=1 AND mi.parentID=1 ORDER BY mi.itemOrder ASC
[1] SELECT itemTitle FROM perch3_menu_items WHERE itemType='app' AND itemValue='content' LIMIT 1
[1] SELECT * FROM perch3_menu_items WHERE itemActive=1 AND itemType='menu' AND parentID=0 ORDER BY itemOrder ASC LIMIT 1, 10
[2] SELECT mi.*, p.privKey FROM perch3_menu_items mi LEFT JOIN perch3_user_privileges p ON mi.privID=p.privID WHERE mi.itemActive=1 AND mi.parentID=2 ORDER BY mi.itemOrder ASC
File: /core/apps/content/modes/edit.form.post.php
Mode: edit.form
Queries: 15
Memory: 3.371

The region is the only thing on the page with any content. It is getting called from a page template, does that make a difference? The pages these descriptions on are templates that have been used to make new pages from the dashboard.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Call the script directly - you need to reduce this to a basic case, eliminating all JavaScript.

This is the debug from the script:

Debug Message - Perch 3.0.8
[nil] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch3_content_regions WHERE regionKey='Description' AND (regionPage='/courses/technology' OR regionPage='*')
No matching content regions found. Check region name (Description) and page path options.
[nil] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch3_content_index idx JOIN perch3_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch3_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE idx.regionID IS NULL AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval, regionID ORDER BY sortval ASC
Template file not found: /Users/nathan/Documents/LOCAL/clients/itfutures/www/perch/templates/content/.html

I tried a region with a different name, and all kinds of different page paths and still nothing. Those values that are in the debig are the correct values that I want. Both the page and the regions are definitely there, and I've hardcoded to values right in there.

Drew McLellan

Drew McLellan 2638 points
Perch Support

The page /courses/technology does not have a region called Description

Are you sure the page isn't /courses/technology.php or /courses/technology/index.php ?

EUREKA!

Adding /index.php has found what I need! I was using the same url structure as my navigation templates, and my .htaccess is removing all php extensions. Thank you for your help!