0

I have a PHP file where when checked boxes are checked and a button is clicked a js file is called that has the following code

$.ajax ({
    type: "POST",
    url: "decisionExec.php",
    dataType: "json",
    data:({cnt:cnt,amGuide:guides.amGuide,folGuide:guides.folGuide,tichGuide:guides.tichGuide,nebGuide:guides.nebGuide,sterGuide:guides.sterGuide,ingGuide: guides.ingGuide }),
    success: function(data) {
        $("#message").html(data.message);//"Generating the files for the BookScouter results.  An email will be sent when this is completed."
        $("#start2").html(data.start);
    } // end of success

}); // end of ajax

this code works fine and sends the correct data to my php file by using POST

 $amG        = $_POST['amGuide'];
 $folG       = $_POST['folGuide'];
 $tichG      = $_POST['tichGuide'];
 $nebG       = $_POST['nebGuide'];
 $sterG      = $_POST['sterGuide'];
 $ingG       = $_POST['ingGuide'];

The problem I am having is that this takes too long so i want to use a passthru file as i do in other areas but cant figure out how to "passthru" the values this is my passthru php file.

 <?php
 passthru('php /decision.php >> /web/webInv.txt &');
 $now = date("m/d/y h:i:s");
 $message = array('message' => 'An email will be sent when it is finished.',
             'start' => 'Started at approx. ' . $now);
 echo json_encode($message);
 ?>

any thoughts on how i can do this (this file takes about 30 minutes to process and email)

  • I don't understand what you're doing. `passthru` sends the output of the command to the client. But you're redirecting the output with `>>`, so there's no output to send. Also, since the command runs in the background, PHP won't wait for the command to finish. – Barmar Apr 01 '16 at 00:07
  • Are you asking how to send the POST parameters to `decision.php`? – Barmar Apr 01 '16 at 00:08

1 Answers1

1

I guess you're asking how to send all the POST parameters to decision.php. You could serialize them with serialize() or json_encode(), and send that as a command line argument to the script.

$args = escapeshellarg(serialize($_POST));
shell_exec("php /decision.php $args >> /web/webInv.txt 2>&1 &");

There's no point in using passthru, since the output is being redirected, so there's nothing to pass through to the client.

In decision.php, use:

$_POST = unserialize($argv[1]);

to get the parameters.

Barmar
  • 596,455
  • 48
  • 393
  • 495
  • i have no problem sending the post parameters to decision.php i can do it the way you suggested, but the problem is that it times out or the user has to sit and wait for the file to generate the file to be email. what i want is to run the decision.php once the button is clicked and runs through the ajax so that the user can go on about their business and an email will be sent to them..if i send it directly decision php they have to stay on the webpage or they dont get an email – Hasan Sulemon Apr 01 '16 at 00:42
  • Running it in the background with `&` should prevent waiting for it to finish. Are you saying that doesn't work? – Barmar Apr 01 '16 at 00:43
  • maybe i put '$args = escapeshellarg(serialize($_POST)); shell_exec("php /decision.php $args >> /web/webInv.txt &");' in the wrong place where should it go – Hasan Sulemon Apr 01 '16 at 01:03
  • What do you mean by the wrong place? It should go in place of your `passthru` line. – Barmar Apr 01 '16 at 01:05
  • I think you also need to redirect stderr with `2>&1`. – Barmar Apr 01 '16 at 01:07