How to Run a PHP Script in the Background After a Form Is Submitted

How can I run a PHP script in the background after a form is submitted?

Doing some experimentation with exec and shell_exec I have uncovered a solution that worked perfectly! I choose to use shell_exec so I can log every notification process that happens (or doesn't). (shell_exec returns as a string and this was easier than using exec, assigning the output to a variable and then opening a file to write to.)

I'm using the following line to invoke the email script:

shell_exec("/path/to/php /path/to/send_notifications.php '".$post_id."' 'alert' >> /path/to/alert_log/paging.log &");

It is important to notice the & at the end of the command (as pointed out by @netcoder). This UNIX command runs a process in the background.

The extra variables surrounded in single quotes after the path to the script are set as $_SERVER['argv'] variables that I can call within my script.

The email script then outputs to my log file using the >> and will output something like this:

[2011-01-07 11:01:26] Alert Notifications Sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 38.71 seconds)
[2011-01-07 11:01:34] CRITICAL ERROR: Alert Notifications NOT sent for http://alerts.illinoisstate.edu/2049 (SCRIPT: 23.12 seconds)

how to show form submitted in php when python code runs in background

To illustrate the comment a very simple ajax request sent, in this case to the same page but would be to input.php, which takes a while to process data ( hence the sleep ) ~ the request is sent but no callback is used or waits for the response - look in the network tab of the console

<?php

if( $_SERVER['REQUEST_METHOD']=='POST' ){
/* to emulate input.php processing request using python */
ob_clean();
sleep(20);
exit( json_encode( $_POST ) );
}

?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>ajax - send and forget</title>
</head>
<body>

<form name='input' action='<?php echo $_SERVER['REQUEST_URI'];?>'>
<input type='text' name='num1' size='4'>
<input type='text' name='num2' size='4'>
<input type='submit' value='Submit' class='submit' />
<div id='msg'></div>
</form>

<script>
const ajax=function( url, query ){
let xhr=new XMLHttpRequest();
xhr.open( 'POST', url, true );
xhr.send( query );
}

document.querySelector('input[type="submit"]').addEventListener('click',e=>{
e.preventDefault();
ajax( e.target.parentNode.action, new FormData( e.target.parentNode ) );
document.getElementById('msg').innerText='Request sent... Game Over';
});
</script>

</body>
</html>

The above is NOT intended to be the code you use - merely to illustrate the use of a send and forget type ajax request. The form ACTION attribute is used in the ajax call - and as I don't have a file called input.php I showed the request to the same page. Your actual input.php will NOT have the sleep function call - perhaps this might be of more use to you... it really is very basic ~ adapt this to your webpage and form.

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>ajax - send and forget</title>
</head>
<body>

<form name='input' action='input.php'>
<input type='text' name='num1' size='4'>
<input type='text' name='num2' size='4'>
<input type='submit' value='Submit' class='submit' />
<div id='msg'></div>
</form>

<script>
const ajax=function( url, query ){
let xhr=new XMLHttpRequest();
xhr.open( 'POST', url, true );
xhr.send( query );
}

document.querySelector('input[type="submit"]').addEventListener('click',e=>{
e.preventDefault();
ajax( e.target.parentNode.action, new FormData( e.target.parentNode ) );
document.getElementById('msg').innerText='Request sent... Game Over';
});
</script>

</body>
</html>

Form to run PHP script and display output on same page

It's so simple to achieve that. Refer the following sample code:

<?php 
if(isset($_POST['submit'])){
//code to be executed
}else{
//code to be executed
}
?>
<html>
<head></head>
<body>
<form method="post" action="">
<input type="submit" name="submit">
</form>
</body>
</html>

The PHP Script will only run if the submit button is clicked.



Related Topics



Leave a reply



Submit