1

I want to call a PHP function from Ajax request. This PHP function will take more than 2 to 3 Min. to execute And user may not want to wait for 2 min. So for this I am planing to make ajax call and redirect to another page so that User can take another request. Till I did -

new Ajax.Request(reloadurl, { 
 method: 'POST', 
 data: $('edit_form').serialize(),
 parameters: $('edit_form').serialize(true)                 
    onComplete: function(transport) {
        //alert(transport.responseText);
        window.location = backurl;  
    },
    onCreate : function () {    
        alert('dsdsdsds');
        window.location = backurl;                          
    },
    onFailure: function() { 
        alert('Something went wrong...');}
    });

But Its not working.. Please help me out- Suggest me the way so that I can call another ajax request simultaneously

3
  • 1
    "It's not working"... Tell us more ! Do you get JS errors ? Do you see alerts ? Commented May 7, 2013 at 13:59
  • 1
    If you use the network tab in Firebug/chrome developer tools does it show a new call being made? Commented May 7, 2013 at 14:00
  • Not getting any Js errors. It's not redirect on onCreate and loading the page till the execution of function. After execution then it redirect on "backurl" Commented May 7, 2013 at 14:02

2 Answers 2

4

The best way to do that is like this:

<?php 

exec('/usr/bin/php /Path/to/script.php  {$param1} {$param2} > /dev/null 2>/dev/null &'); 

?>

And then in script.php you can get the parameters like this:

print_r($argv); //print all parameters
echo $argv[0]; //first parameter

This will run the php script as a separate thread and not require the user to wait for the output.

I tend to update a file or db to keep track of how the running of the script is going.

Sign up to request clarification or add additional context in comments.

4 Comments

Ya! I know it but I dont want to call exec function or the Cron Job. Because Its a server settings and client don't want to do that.. Is there is another way to do that using Ajax?
Then the code you have above will wait for the script to execute, that's when the header data will be sent back to your JS.
Ok. Could you please tell me more on exce function. I mean, I called process() function present in process.php file. I submitted all data to process.php file via POST method to Process() function. So where does I write the exec call? Please help me....
In process.php you would run exec(... In script.php (or whatever you call it) you would put whatever is in process.php now
0

Your approach could work, but if you use sessions, you should close manually with session_write_close() as soon as you won't write to it any more.

... session data is locked to prevent concurrent writes only one script may operate on a session at any time

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.