Forum

Thread tagged as: Question, Problem, Add-on-development

Functions in runtime.php needing to be accessed via AJAX

Hi guys,

I'm trying to develop a really small app for Perch, and I'm having a little trouble.

Essentially, once a button on a page is clicked, I'll need to run a function included in runtime.php, and add/remove some stuff from a database.

I've not had much luck so far - I can't seem to access any Perch functionality (e.g Perch constants) when accessing the function using ajax to send a POST request to /cms/addons/apps/my_app/runtime.php.

Has anyone got any tips for running functions through AJAX like this?

Harry Ray

Harry Ray 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

There's a good example in the Twitter app:

perch_twitter/async/tweet-length/index.php

Hi Drew,

Thanks for that - I'm not too sure what's going on with that file but I'm using the realpath code to include my runtime.php file now.

The way that this function is currently working is:

  1. User clicks button on front-end of website.

  2. AJAX Request to /perch/addons/apps/my_app/user_interaction.php.

  3. This file runs a function that's contained within runtime.php, which is included via realpath().

My Class files are called inside runtime.php. However, when I try to run this process through, I get a 500 Error, detailed below, PHP Fatal error: Class 'PerchAPI_Factory' not found.

Any ideas on this one?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you including the Perch runtime, or your app's? You need the central Perch runtime.

I'm including my app's runtime.php - I figured I probably needed to include some kind of Perch core file but when I tried to use include('../../../core/inc/api.php'); in my runtime.php yesterday, it didn't work and indicated the I wasn't able to access that file due to a permissions error (presumably because I wasn't logged in to Perch?).

I've updated my app's runtime.php to start with include_once($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');, as just using include threw the "Included more than once" error.

Currently, I've got an issue with my database statements, looks like my connection is failing - but I don't think this is a Perch issue.

Thanks for all the help here, Drew!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Include the /perch/runtime.php file. You don't need to include anything else - it's then done for you.