I have a php script that works just fine when it's run via SSH in the terminal. However, my goal is to have a button on a web page that will trigger the same script to run.
Let's call the script foo.php and when passed with parameters will be foo.php -b 35
Then my web page will have a form that would run this script.
<form action="foo.php" method="post" target="_blank">
Answers
To run a PHP script from a web page using a form, you can indeed use the <form>
tag with the action
attribute set to the URL of your PHP script and the method
attribute set to "post" or "get" depending on how you want to pass parameters. However, there are a few things you need to consider:
- Ensure that your web server is configured to execute PHP scripts.
- Make sure that the PHP script is located in a directory that is accessible to the web server.
- Check the file permissions to ensure that the web server has permission to execute the PHP script.
Here's an example of how you can create a form to execute your PHP script:
<form action="foo.php" method="post" target="_blank">
<input type="hidden" name="-b" value="35">
<input type="submit" value="Run Script">
</form>
In this example:
- The
action
attribute specifies the URL of the PHP script (foo.php
). - The
method
attribute is set to "post" to send the parameters as part of the request body. - The
-b
parameter is passed as a hidden input field with the value "35". - The
target="_blank"
attribute ensures that the script opens in a new window or tab.
Make sure to adjust the form according to your specific requirements and environment. Also, ensure that any user input passed to the PHP script is properly validated and sanitized to prevent security vulnerabilities.