AJAX stands for Asynchronous JavaScript and XML. AJAX is an advanced javascript technique for creating faster and more interactive web apps with the help of HTML, CSS, and JavaScript.
Recently, I need to make an ajax call without waiting for a response and I have found some examples on the internet but none were clearly explained. so, I decided to write a blog post for jQuery ajax call without waiting for a response in PHP.
Let’s, See the example
1) INDEX.PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<script src="http://code.jquery.com/jquery-3.4.1.min.js" crossorigin="anonymous"> </script> <input type="button" value="File Write Now" id="file_write"> <script> $(document).on('click', '#file_write', function () { $.ajax({ url: "ajaxBack.php", type: "POST", data: "", cache: false, async:false, success: function (result){ } }); }); </script> |
In the above, we have included one jquery and added one button for ajax calls. When you click on the “File Write Now” button ajax call will fire on ajaxBack.php
2) ajaxBack.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php ignore_user_abort(false); set_time_limit(0); ob_start(); echo "1"; // send the response header('Connection: close'); header('Content-Length: '.ob_get_length()); ob_end_flush(); ob_flush(); flush(); for ($i = 1; $i < 20; $i++) { sleep(3); $myfile = fopen("code.txt", "a") or die("Unable to open file!"); $txt = ('Test script got to ' . (10 * $i) . ' seconds./n'); fwrite($myfile, $txt); } ?> |
ignore_user_abort(false);
set_time_limit(0);
We have set the above variables to Ignore user aborts and allow the script to run forever.
header(‘Connection: close’);
After sending the response message, the connection gets closed. you need to include it in the header if you want your connection to close.
header(‘Content-Length: ‘.ob_get_length());
HTTP Content-Length header is used to indicate the size of data in the body of the request.
ob_end_flush();
This function will delete the topmost output buffer and turn this output buffer off.
ob_flush();
ob_flush passes the current content to the upper layer and flushes output buffers you created with a function like ob_start
flush();
flushes buffered output of the PHP script itself to its caller.
Now, our loop run 20 times and write one text file with test data. in between writing the data you can close your browser tab. still, data should be written in a txt file. The response will send instantly even background process is running.
So, in the jQuery Ajax Call Without Waiting For Response in PHP article, our main motive is we need an instant response without waiting for script execution.
I hope it helped you to fix error 1292 – Incorrect date value: ‘0000-00-00’ for column ‘date’. Please let us know in the comment section if you have any concerns.
Thank You!
Thanks for your time, to know about me.
Hello I Am Vijay Poshiya. 🙂
Here’s My little description of your attention on Me and My blog. I am here to help you with PHP programming. I can give you a cake walkthrough platform where a complex program can make it easier for you. I will also provide you with the rare collections of task sets over the internet.
I hope you will find your solutions in better understanding shape within my blog.
You can read more about PHPAdvices at the “About” page.
Leave a Comment