I have a dropdown box. When the user selects a value from the dropdown box, it performs a query to retrieve the data from the database, and shows the results in the front end using ajax. It takes a little bit of time, so during this time, I want to show a progress bar. I have searched, and I have found numerous tutorials on creating progress bars for uploads, but I haven't liked any. Can anybody provides some helpful guidance for me?
My ajax code:
<script> $(function() { $("#client").on("change", function() { var clientid=$("#client").val(); $.ajax({ type:"post", url:"clientnetworkpricelist/yourfile.php", data:"title="+clientid, success:function(data){ $("#result").html(data); } }); }); }); </script>
Use the ajaxStart to start your progress bar code. $(document). ajaxStop(function(){ // hide the progress bar $("#progressDialog"). modal('hide'); });
ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.
GET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.
This link describes how you can add a progress event listener to the xhr object using jquery.
$.ajax({ xhr: function() { var xhr = new window.XMLHttpRequest(); // Upload progress xhr.upload.addEventListener("progress", function(evt){ if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; //Do something with upload progress console.log(percentComplete); } }, false); // Download progress xhr.addEventListener("progress", function(evt){ if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; // Do something with download progress console.log(percentComplete); } }, false); return xhr; }, type: 'POST', url: "/", data: {}, success: function(data){ // Do something success-ish } });
<script> $(function() { $("#client").on("change", function() { var clientid=$("#client").val(); //show the loading div here $.ajax({ type:"post", url:"clientnetworkpricelist/yourfile.php", data:"title="+clientid, success:function(data){ $("#result").html(data); //hide the loading div here } }); }); }); </script>
Or you can also do this:
$(document).ajaxStart(function() { // show loader on start $("#loader").css("display","block"); }).ajaxSuccess(function() { // hide loader on success $("#loader").css("display","none"); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With