I am trying to call a PHP file from a form button. I put the file to be called into the asset folder of the Kirby site and calling it works well so far. However, I don’t seem to be able to call any Kirby functions from that PHP file like $kirby->url() or $kirby->option(‘someOtherSetting’);
Can Kirby functions only be used by files in the site folder? If so, can I put my PHP to be called in there as well? So far whenever I have tried that I get a server error.
I am having a bit of trouble wrapping my head around how that would work.
So my button is calling a route and the route then executes my script? (My PHP essentially calls a Python script via exec() and passes along the form input.)
So I can write my PHP in a route? Or would the route call my PHP script? And how do I pass along my form fields?
Actually, I didn‘t say Panel. The button lives on the homepage.
Ok, here is the full description of what I want to do. I have a form field that takes a URL and a button that submits that URL. I also have a Python script that runs stuff based on that URL. I‘d like to connect that button with the Python script.
I do have a rudimentary version working by calling a PHP script that lives in /assets/php/takes_form_input.php. The PHP then calls my Python script that lives in /assets/python/does_stuff.py. But right now all the paths are hardcoded in the takes_form_input.php and I‘d like to use the dynamic paths Kirby gives me.
At least it would be nice to have. As I said, it is working right now, but hardcoded paths are not ideal.
I cannot get my controller to work. I copied the contact form example and paired it way down. But it does not seem to update my page when I hit submit.
My controller simply sets the submitted variable. So it should always succeed.
<?php
return function($kirby, $pages, $page) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
$data = [
'url' => get('url')
];
try {
$url = $data['url'];
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = 'The form could not be sent: <strong>' . $error->getMessage() . '</strong>';
else:
$alert['error'] = 'The form could not be sent!';
endif;
}
// no exception occurred, let's send a success message
if (empty($alert) === true) {
$success = 'Your message has been sent, thank you. We will get back to you soon!';
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
?>