php

embluk

·

Run a File From Button Click

·

PHP

·

Total Size: 2.26 KB

·

·

Created: 6 months ago

·

Edited: 6 months ago

<?php // Using the GET method: // Check the GET header was set and is not empty if(isset($_GET["run"]) && !empty($_GET["run"])) { // You are free to add multiple cases here... switch($_GET["run"]) { // So this could match: index.php?run=1 case 1: // Then you can use shell_exec to run the .py file like so: $command = escapeshellcmd('/usr/custom/test.py'); $output = shell_exec($command); break; } } // Using the POST method: // Same sort of thing really, just uses post in the if statment to check. // This will refresh the page though in order to get the PHP code to run... if(isset($_POST["run"]) && !empty($_POST["run"])) { // Same code above will go here... } // But to perform a POST request, check the notes section on the right:

So you have your buttons right, I am guessing they are inside of some HTML form?

If not, then you will need to in order to fire a GET or POST request, here is an example of a GET form:

 <form action="/action_page.php" method="get">
  <input type="submit" value="Button 1">
</form> 

Now, when clicking the submit button on the form, it will send of a GET request. In order to change it to a POST request, you change the method to 'POST' like so:

 <form action="/action_page.php" method="post">
  <input type="submit" value="Button 2">
</form> 

You can make things more complex by using Ajax to send off a request in the background and not let the page be refreshed but I think you want it kept simple for now.

Here are some links you should look at and read:

And some more info on Switch statements:

https://www.w3schools.com/php/php_switch.asp

1 bit

130 views

Are you sure you want to delete?