Calling a php file from a form button

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.

Why don’t you create a route? All Kirby’s internals will be available.

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?

Still need to do some more studying I think.

it all depends on what you’ld like to do…

Passing form fields etc was not in the original request of “running a php script on the push of a button in panel”. Maybe what you want can be done with GitHub - bnomei/kirby3-janitor: Kirby Plugin for running commands like cleaning the cache from within the Panel, PHP code, CLI or a cronjob ?

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.

Hi, ideally when you use forms to submit data to Kirby, you’ld want a controller (or route) to process that request.

See Controllers | Kirby CMS for more info around controllers. See above for the link to the docs for routes.

This might be useful too: Contact form | Kirby CMS (you don’t want to send an email, but run your python script)

Cheers. I‘ll look into it.

That looks exactly like what I needed. Thanks. All this controller and routes stuff is new to me. Until recently my knowledge was Kirby 2. :slight_smile:

But I will get there.

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
 	    ];
	};
			


?>

and on my homepage I have this bit:


    <?php if($success){ ?>
    <div class="alert success">
        <p><?= $success ?></p>
    </div>
    <?php } elseif (isset($alert['error'])) { ?>
        <div><?= $alert['error'] ?></div>
	<?php } ?>
	
  	<form action="<?= $page->url() ?>" method="POST">
  		<label for="url">URL:</label>
  		<input type="text" id="url" name="url" value="<?= esc($data['url'] ?? '', 'attr') ?>" required>
  		<input type="submit">
  	</form>
	

I expected the success div to appear after my submit. Is that not how it is supposed to work?

Your submit input has no name attribute, therefore, the first condition in the controller will never be true.

Thanks. I just figured it out myself as well. :slight_smile: