I have a process defined in a batch file that runs 3 php scripts, one after another in sequence, and I want to create a web front-end for the process. The process is triggered when someone uploads a file using a webform.
I want the ability to notify the user after each script in the batch file is run with some useful messages, but I am not quite sure what the right way to go about is.
I am thinking in terms of javascript that sends request to the 3 php files in sequence, with the php scripts echoing the status messages as they are executed. But I would rather have the three files executed in a single trigger from the client instead of having javascri开发者_StackOverflowpt calling the the three scripts separately.
Any ideas whats the best way to go about it?
You could create a single php file that runs the 3 php script and echoes their ouputs. By executing a server side request trough AJAX (I suggest jQuery framework) the outputs may be collected and the client side scripts can process and show the results to the user.
Store messages in the database or other centralized storage system. Use an ajax call to pull the newest messages.
Send an ajax call to a php file on your server. You don't specify how your php scripts are running, but what I would do is include other the other php files (which would have functions to process whatever they're supposed to do) and then call the functions from the main file. These functions should return some sort of success/error messages that you can collect in an array. So something like this:
$response[] = firstFileProcessing(); //your function names should be better
$response[] = secondFileProcessing();
$response[] = thirdFileProcessing();
When all the scripts are done processing, do:
echo json_encode(array("responses" => $response));
Then back in your success function for the ajax call, you'd do:
var res = jQuery.parseJSON(response_from_server);
//or if you told ajax to expect a json response,
//response_from_server would automatically be an object based on the json sent
and step through each one to see what php said.
alert(res.responses[0]); //the first file said this
Or you could make your $response from php much more detailed - an array of its own, with $response['success'] = TRUE, $response['msg'] = "Worked!", etc - any amount of data.
精彩评论