Currently I have 2 PHP files. 1 is the user interface and another fetches data from the backend and inserts into the database.
Currently if I use the following in the UI php:
<html>
<body>
<form action = "test.php" name="form" method="post">
<input type="text" name="text_box" size="50"/>
<input type="submit" id="search-submit" value="submit"/>
</form>
</body>
</html>
Upon clicking submit it goes to the test.php. If it possible to execute test.php in the background while remaining on the UI php?
Some of the previous posts talk about using ajax etc which I am not sure how to implement. Possible to do this in php?
In test.php you can use the exec function and call whatever php file you want using php by command line:
exec("php test.php > /dev/null 2>/dev/null &");
Just notice that the session that it will have is not the same as what you have on the browser, and it can be tricky to send parameters to the command line instance that is being initiated, take a look here.
Related
I have an overhead controller (sort of a magic wand for some robots) plugged into my usb port.
I am trying to create a web server on linux that allows users to send a preloaded code (so no one is inputting anything) to the robots. I have embedded the command in a script file that I am trying to execute in a php file. The bit of code that executes the script file in php is run when a button is pressed on the web server. However, when I press the button nothing happens. The code doesn't get sent to the controller and no errors are given.
I can run the script in the command line with no problems. But when I try to execute it run it from the web server, nothing happens. Is there a way I can resolve this?
This is what's in the php file
<html>
<body>
<h1><font face="verdana" color="green">SwarmArena</font></h1>
<hr>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type ="text" name = "name"/>. ; <br>
Age: <input type ="text" name="age"/>. ; <br>
New Code: <textarea cols=40 rows=10 name="Newcode">Default text</textar$
<input type = "submit" name="enter" value="Enter" />
<input type = "submit" name="insert" value="insert" />
<input type = "submit" name="select" value="select" />
<input type = "submit" name="sleep" value="sleep" />
<input type = "submit" name="reset" value="reset" />
</form>
</body>
</html>
<?php
if(isset($_POST['sleep'])){
$out = shell_exec('./sleep.sh');
echo "$out";
}
?>
this is what's in the sleep.sh
#!/bin/sh
cd kilocmd && ./kcmd -s /dev/ttyUSB0 -c sleep
echo "done";
Thanks
I don't know how you are doing this. But onclick the button it should submit to a php page which will run the shell scripts using exec and shell_exec php commands .
More Over that Several Possibilities May Cause Issues like
- exec and shell_exec are disabled in php.ini
- The path to the executable is wrong. If the script is in the same
directory as the php file, try exec(dirname(__FILE__) .
'/myscript.sh');
Assuming you are executing the command correctly from the PHP script, it may be useful to look into permission issues.
Log into the user that the web-server is running under and try executing the command manually from there. I personally have my webserver running under a user named apache.
To view your current user run whoami in the command line.
To change user run su - newuserhere.
If this works, or you are running the web-server under root then you can most likely rule-out any permission issues.
I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.
I am using the following code to try and execute two different commands from button clicks in a php webpage.
<?php
if (isset($_POST['button'])) { exec('/usr/local/bin/node desktop/server.js'); } ?>
<form method="POST">
<p>
<input type="hidden" name="button" value="1">
<input type="submit" value="Start Video">
</p>
</form>
<?php
if (isset($_POST['button'])) { exec('/usr/local/bin/node desktop/test.js'); } ?>
<form method="POST">
<p>
<input type="hidden" name="button" value="1">
<input type="submit" value="Test">
</p>
</form>
I can get the first command to execute ok to start the server and it works fine but when I then click on the second one nothing happens. I have tried each of them just on the webpage individually and they both work fine on their own but what i want to do is start the server then run the other script, but it won't allow me for some reason?
Please note that nodejs applications normally don't stop. And that exec will wait for the end of the called program. The combination of both will freeze PHP until the nodejs process has finished. Depending on your web server, there can be more or fewer simultaneous PHP processes or threads, but normally this number is limited, so you should avoid this situation. And there is another reason to avoid this situation: Your browser will trying to load the page forever (until timeout) because it never receives the end of the page.
You can simply avoid this problem, if you append an ampersand to the end of the command line:
exec('/usr/local/bin/node desktop/server.js &');
This will make the nodejs progress starting in background, and your PHP script can continue.
I have a php script whose name is XYZ.php. When I load that file in browser it executes a form every 5 seconds automatically. I want cron to run this script every hour and I don't have any problem in setting up cron job in cpanel but I don't know how to write php codes in XYZ.php file to work with cron.
In my php file there is one form which executes random name from database every hour and submit it to database once again.
So in form there is only one field i.e name of users which needs to be run from cron.
Please guide how can I do that. If I can open that file for 30 seconds using cron then it will do the work I am looking for.
Edit:
This is my form javascript will automatically submit it in 5 seconds so how to execute this script from cron
<form name="form1" method="post" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>">
<input name="lastlogin" type="text" id="lastlogin" value="<?php echo (time()); ?>" size="40" />
<input type="submit" name="Submit" value="submit" />
</form>
cron is a tool designed to execute command line programs. You appear to have a JavaScript-based web application: you simply can't use that. While you'd be able to retrieve the page using a command line web browser, the dynamic part would not be executed.
The obvious answer is to write a command line PHP script. There's a full chapter about it in the PHP manual: Using PHP from the command line. I recommend this section:
http://es.php.net/manual/en/features.commandline.usage.php
You don't need a form post at all. Have your script file do the database work when executed. Then have your cron job be:
php XYZ.php
If you want the database work to be done from a cron job and when a user goes to a web page and submits a form, create a class in XYZ.php that does the work. Then have a script called XYZ-cron.php that creates the class and does the work. Then create a script ran by Apache called XYZ-form.php that renders a form and handles POST requests to do the same work, using the class from XYZ.php.
I have a form that when the person selects the date I want the value to go into a php function.
I can view the value by using this.form.orderdate.value, but I want to put that value into the php variable $dateChosen
Here is the page so far:
TDM KML Generator
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<script type="text/javascript">DateInput('orderdate', true, 'YYMMDD')</script>
<input name="submit" type="submit" value="get KML" />
</form>
</body>
Javascript is all executing on the client machine, with the exception of an ajax call (which is still browser/client side and is making a request to a server-side function). PHP happens all server-side. In your case, you're trying to do something that isn't possible, because the PHP script is no longer executing by the time the page is rendered in the browser and the javascript is able to execute.