Data Load While Page Scrolling (infinite scroll) with jQuery PHP and MySQL - php

I have infinite scroll and on top of this I have ( list of categories ) so when one of these buttons clicked the infinite scroll updated and new information loaded by ajax, after the data loaded the infinite scroll stop working
$(document).ready(function() {
// Infinite Ajax Scroll configuration
jQuery.ias({
container : '#events', // main container where data goes to append
item: '.items', // single items
pagination: '.event-nav', // page navigation
next: '.event-nav a', // next page selector
loader: '', // loading gif
triggerPageThreshold: 5 // show load more if scroll more than this
});
});
//Set up click filterBy event
$(document).on('click', '.filterBy' ,function (event) {
$( ".filterBy" ).removeClass('active');
$(this).addClass('active');
var url = '/rest/ajax/filter.json';
var divId = "event-category";
//Process button click event
loadAjax(this.id,url,divId);
});
//Set up click filterType event
$(document).on('click', '.filterType' ,function (event) {
$( ".filterType" ).removeClass('active');
$(this).addClass('active');
var url = '/rest/ajax/filterType.json';
var divId = "events";
//Process button click event
loadAjax(this.id,url,divId);
});
// JavaScript Document
function loadAjax(val,urlReq,divId)
{
$.ajax({
type: 'POST', // type of request either Get or Post
url: urlReq,// Url of the page where to post data and receive response
data: 'data='+val, // data to be post
beforeSend: function( xhr )
{
$( "#"+divId ).show();
$('html,body').animate({
scrollTop: $("#"+divId).offset().top
}, 1500);
$("#"+divId).html('loading.........');
},
success: function(data)
{
$("#"+divId).html(data);
} //function to be called on successful reply from server
});
}

Related

When refreshing a div Morris charts are disappear

I want my charts and page data has to be changed at certain time without refreshing the page.
I used an ajax request to reload the page or specific div, but when I do that morris.js charts are disappearing. Here is my ajax request. (actually I want full page refresh but if I do that it'll blink page and can't navigate through the page even if I change interval time to be longer).
function updateShouts() {
var selectedAds = 1;
var div = $('#refresh').html();
$.ajax({
url: "<?= base_url() ?>owner/dashBoard",
type: 'POST',
data: {
selectedAds: selectedAds
},
success: function(data) {
if (status) {
/* $("#refresh").load(location.href + " #refresh");*/
location.reload();
}
}
});
}
setInterval("updateShouts()", 4000);
updateShouts();

send parameters to bootstrap modal

I have number of links that open bootstrap modal popup:
Edit 15
Edit 32
the problem - when the modal open it should get the link parameters:
data-act="edit" data-itinID="5" data-secID="15"
Well, it's not happening...
The script in the links page is:
<script>
// send data to modal file
$('#manageSec-model').on('show.bs.modal', function (e) {
var button = $(e.relatedTarget);
var act = button.data('act');
var itinID = button.data('itinid');
var secID = button.data('secid');
var date = button.data('date');
var $modal = $(this);
var info = 'act=' + act + "&itinID=" + itinID + "&secID=" + secID + "&date=" + date;
// alert(info);
$.ajax({
cache: false,
type: 'GET',
url: 'itinPage-secManage.view.php',
data: info,
success: function(data) {
$modal.find('.modal-body').html(data);
setTimeout(function(){ //added this line.
setImageUploader()
})
}
});
});
</script>
Modal:
echo "itinID: ".$_GET['itinID']." secID: ".$_GET['secID'];
Result in Modal:
itinID: secID:
itinID: 5 secID: 15
two lines (not sure why...)
for this to work you will have to call ajax on click of the anchor tag and for passing the values to ajax you can do following on click of anchor tag
$(this).attr("id");
or
$(this).attr("data-secID");//haven't tried this 2nd one
and then launch modal with JavaScript or jQuery with
$("#someId").modal();
Try changing
var button = $(e.relatedTarget);
to
var button = $(e.target);

add waiting message before load remote data in bootstrap modal box

I have this code for load remote php data into bootstrap modal box:
$(function() {
$(window).bind("resize", function() {
if ($(this).width() < 400) {
$('#tabs-content').removeClass('col-xs-10').addClass('col-xs-12')
} else {
$('#tabs-content').removeClass('col-xs-12').addClass('col-xs-10')
}
}).resize();
});
$(function() {
$(document).on('click', '.push', function(e) {
e.preventDefault();
var id = $(this).attr('id');
$.ajax({
type: 'post',
url: 'details.php', // in here you should put your query
data: {
'bookid': id,
'lang': 'fa',
'csrf_token': 'MTQ0OTQxNDQ0MUVvN2JwNXNJRHVxMDZmOXFpQm1ROFNNTk1UZ3lPMGZO'
},
success: function(r) {
// now you can show output in your modal
$('#bookdetails').modal({
backdrop: 'static',
keyboard: false
}) // put your modal id
$('.something').show().html(r);
}
});
});
});
This worked for me But I need to show loading message/image before load data.
How do add waiting message/icon?!
You just need to show image/message before Ajax call and hide it in success: function(r)
Assuming you have image which to show before modal load, image HTML e.g
<img class="progress" src="http://downgraf.com/wp-content/uploads/2014/09/01-progress.gif" style="display:none;">
and in JS, just show image with .show() function and after modal load in success: function(r) hide is with .hide() function
$(document).on('click', '.push', function(e) {
e.preventDefault();
var id = $(this).attr('id');
$(".progress").show(); // <-- Show progress image
$.ajax({
type: 'post',
url: 'details.php', // in here you should put your query
data: {
'bookid': id,
'lang': 'fa',
'csrf_token': 'MTQ0OTQxNDQ0MUVvN2JwNXNJRHVxMDZmOXFpQm1ROFNNTk1UZ3lPMGZO'
},
success: function(r) {
// now you can show output in your modal
$('#bookdetails').modal({
backdrop: 'static',
keyboard: false
}) // put your modal id
$('.something').show().html(r);
$(".progress").hide(); // <-- Hide progress image
}
});
});
Minimal example with delay and fade

HTML form submit not working after being loaded with ajax

so I am loading a portion of a page using jquery/ajax.
On the page the user sees, there is a "menu" where they select the date of the signup form they want to see. All the forms are hosted on another page, each one inside a div id'd with the respective date of the form. When the user clicks and item on the menu, there is an ajax call that displays the correct form on the user's page, pulling it from the other page by it's parent div and id.
The plugin I am using for the signup forms (it is a Wordpress site) has the page reload when you click Sign up, which then takes you to a form to fill out. I have it so that the user's page does not reload, but via ajax shows the form. This all works great - the only problem now is when the user clicks to submit the form. This should be a normal form submit not using ajax, as I am not sure how to modify the plugin code to utilize it. For some reason, the form is never actually submitted although the user's page does reload.
*NOTE: I am currently using the same exact signup form for each date, but once it is functional it will be a different signup form for each. This should not effect any functionality.
link to page user sees: summitsharks.net/volunteer-signup-page
link to page forms are hosted on: summitsharks.net/formhost
jquery/ajax code:
;(function($){
var ahref1;
$(document).ready(function(){
$(document).on('click', '.entry-content li a', function(e){
e.preventDefault();
ahref1 = $(this).attr('href');
$('#formloader').load('/formhost ' + ahref1);
return false;
});
});
$(document).ready(function(){
$(document).on('click', '.entry-content #formloader a', function(e){
e.preventDefault();
var ahref2 = $(this).attr('href');
$('#formloader').load(ahref2 + ' ' + ahref1);
return false;
});
});
})(jQuery);
PHP code of file that (I think) handles form submit:
http://pastebin.com/PeXB4Afi
I am looking for a solution that successfully signs the user up. If somebody knows how to alter the plugin code to accept ajax submission, or normal submission that actually works, either one is perfectly fine with me.
Thanks a lot for looking through and thanks in advance for your help!
The form is expected to be posted from it's original URL, including the HTTP GET parameters ?sheet_id=1&task_id=1&date=2016-06-30. Updating the form's action attribute to make it post to the proper URL can be done by changing
$('#formloader').load(ahref2 + ' ' + ahref1);
to
$('#formloader').load(ahref2 + ' ' + ahref1, function() {
$('#formloader form').attr("action", ahref2 + ' ' + ahref1 );
});
However, using AJAX to post the form, this can be skipped:
var ahref = $(this).attr('href') + ' ' + ahref1;
$('#formloader').load( ahref, function() {
$("#formloader form").on('submit', function(e) {
e.preventDefault();
$.ajax( {
url: ahref,
type: 'POST',
data: $.param( formdata( $(this) ) ),
success:function(data,status,jqXHR) { $("#formloader").html( data ) }
});
return false;
})
})
The utility method formdata (see code snippet below) converts jQuery's serializeArray() result to a proper hash.
In the working example below, I've moved the installation of form click handlers into the .load completion handler, rather than relying on jQuery to fire a second document ready event after injecting the form.
;jQuery(function($) {
$('.entry-content li a').off('click').on('click', function(e) {
var ahref1 = $(this).attr('href');
$('#formloader').load( "/formhost " + ahref1, function() {
$('.entry-content #formloader a').off('click').on('click', function(e) {
e.preventDefault();
var ahref = $(this).attr('href') + ' ' + ahref1;
$('#formloader').load( ahref, function() {
$("#formloader form").on('submit', function(e) {
e.preventDefault();
$.ajax( {
url: ahref,
type: 'POST',
data: $.param( formdata( $(this) ) ),
success:function(data,status,jqXHR) { $("#formloader").html( data ) }
});
return false;
})
});
return false;
});
});
return false;
});
});
function formdata(form) {
var data = {};
for ( var i in d = form.serializeArray() )
data[d[i].name] = d[i].value;
return data;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
UPDATE: Here is a code snippet that can be pasted in the browser's Javascript console:
$ = jQuery;
$('.menu-volunteermenu-container li a').off('click').on('click', function (e) {
loadFormSelector($(this).attr('href'));
return false;
});
$('#formloader').on('load', function(){console.log("FORMLOADER UPDATD")});
function loadFormSelector(ahref1)
{
console.log("Loading form selector");
$('#formloader').load('/formhost ' + ahref1, function ()
{
console.log('form selector loaded');
$('.entry-content #formloader a').off('click').on('click', function (e)
{
e.preventDefault();
loadForm(ahref1, $(this).attr('href') );
return false;
});
});
}
function loadForm(ahref1, ahref2)
{
var ahref = ahref2 + ' ' + ahref1;
console.log('Loading form', ahref);
$('#formloader').load(ahref, function () {
console.log('form loaded', arguments);
$('#formloader form').on('submit', function (e) {
e.preventDefault();
$.ajax({
url: ahref,
type: 'POST',
data: $.param(formdata($(this))),
success: function (data, status, jqXHR) {
$('#formloader').html( $(data).find( ahref1 ) )
}
});
return false;
});
$('#formloader a').on('click', function () {
loadFormSelector(ahref1);
});
return false;
});
}
function formdata(form) {
var data = {
};
for (var i in d = form.serializeArray())
data[d[i].name] = d[i].value;
return data;
}
It is refactored to show the 2-layer approach more clearly.

jQuery Live Search keyup show loading

I am trying to display a loading animation and hide the search input box before the ajax request has been sent, the ".se-pre-con" element is the loader.
As you can see below I am tring to show this in the beforeSend of the ajax request. However, when the user is typing in the search box then stops for the delay specified (1000ms) the input box still displays throughout the ajax request but it is unresponsive.
Then the results are returned, and the loading animation does the fadeOut so it the show() must be triggering but the page remains unchanged until the results are returned. I want to prevent the unresponsive page effect and show a loading animation during the request. Any help is much appreciated.
$(document).ready(function() {
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
$('#search_field').keyup(function() {
var target = $(this);
delay(function() {
getSearchResults(target.val());
}, 1000);
});
function getSearchResults(str) {
$.ajax({
beforeSend: function(){
$(".se-pre-con").show();
$("#search_field").hide();
},
url: "http://example.com/Search_Results.php",
dataType:"html",
data: {"search_term": str},
method: "post",
async: false,
success: function(data){
if(data !== null) {
$("#search_default").hide();
$("#search_results_wrapper").html(data);
}
$(".se-pre-con").fadeOut("slow");
$("#search_field").show();
$("#search_field").focus();
},
error: function(){
$(".se-pre-con").fadeOut("slow");
$("#search_field").show();
$("#search_field").focus();
}
});
}
});
I found the solution.
Solution
It started working when I removed the option:
async: false

Categories