jquery - How to go from one page to another page when Ajax call is on in PHP?

612

How to continue run ajax call after redirect to another page in PHP?

How to go from one page to another page when Ajax call is on in PHP?

My ajax call is performing a very complex task and it will take a longtime So, Is it possible to run an ajax call after page redirect?

I make ajax call on submit button click and after that page redirects on another page and then my ajax call is Continue Running in Backend.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <title></title>
</head>
<body>
  <input type="text" name="altname" id="altname">
  <button id="btnSubmit">Submit</button>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(){
    $(document).on('click','#btnSubmit',function(){ 
    var altname = $("#altname").val();
        $.ajax({
            url:"http://localhost/z/backend-queue-job-php/queue_job.php",
            type:"POST",
            data:{altname:altname},
            success:function(result)
            {   
                alert(result.msg);
            }
        });
        window.location.href = "http://localhost/z/backend-queue-job-php/thankyoupage.php";
    });
}); 
</script>
281

Answer

Solution:

The simpler way to run a PHP script in background is with exec or shell_exec

You can run following function in a php file which is called via ajax, passing the path of your current script with a log path if you want to store log info:

shell_exec("/path/to/php /path/to/queue_job.php > /path_to_log/debug.log 2>&1");
156

Answer

Solution:

It can only be done using async task in your PHP part.

  • Assign an ID for every task
  • Your ajax will create new task or fetch the status of existing task

People are also looking for solutions to the problem: html - What is the levels column in roles in my database for PHP laravel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.