How to show loading image at load() proccess with js - php

I want to show loading image in my page..
I have a load() function and i need to display a loading image in the center of my page while its loading and i am using load() with backbone router.
I think i need javascript to do it but i dont know how to show it only at load() proccess.

**You can use in this way**
function showLoadingMsg() {
$('#loading-message').css({
display : 'block'
});
}
function hideLoadingMsg() {
$('#loading-message').remove();
}
**the place where you want to show loading gif**
<!--loding image goes here-->
<div style="display:none" id="loading-message"><img src="<?php echo base_url(); ?>assests/img/loading.gif" /></div>
**When to show and hide should be in**
obj.fbId ="14232";
showLoadingMsg();
$.post("<?= base_url() ?>welcome/insertVideos",obj,
function(success){
hideLoadingMsg();
}, "json");

I am also php developer i did this using jquery..
You can use it through Jquery like this:-
Just confirm your jquery script are present..
<div id="popup1" class="popup_block">
<?php echo $this->Html->image('loader2.gif'); ?>
<h4>Processing Please Wait...! </h4>
</div>
And you have to call this function
function open_loader(){
if(enrollFlag==0){
return false;
}
//Fade in Background
jQuery('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
jQuery('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer
var popID = 'popup1';
//var popWidth = 500;
//Fade in the Popup and add close button
jQuery('#' + popID).css({ 'width': '250' });
//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
var popMargTop = (jQuery('#' + popID).height() + 80) / 2;
var popMargLeft = (jQuery('#' + popID).width() + 80) / 2;
//Apply Margin to Popup
jQuery('#' + popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft,
'display' : 'block'
});
}
This function will show loader image when it will execute..
Or if you stick to load()
do like this:-
<input type="button" onclick="example_request()" value="Click Me!" />
<div id="example-placeholder">
<p>Placeholding text</p>
</div>
function example_ajax_request() {
$('#example-placeholder').html('<p><img src="/images/loader.gif" width="220" height="19" /></p>');
$('#example-placeholder').load("/examples/loaded.html");
}

Lets say you have a div with image tag in it.. If you have ajax request than You can show or hide the div like this... (place this code on .ready function)
$('#loadingDiv')
.hide() // hide it initially
.ajaxStart(function() {
$(this).show();
})
.ajaxStop(function() {
$(this).hide();
});
And if you do not have ajax call than use like this....
function Load()
{
$('#loadingDiv').hide(); // hide it initially
$('#loadingDiv').show() ;
//-------------Your code here of form loading------
//-------------Your code here of form loading------
//-------
//-------
$('#loadingDiv').hide();// hide it in the last
}

Related

JQuery AJAX and php gallery slide show

I create a gallery with Jquery and it worked fine, later on I decided to get the file from directory and not from tag.
I used AJAX and PHP, I get the images into the gallery div but the css class not influence the the gallery to make it work.
html
<div id="gallery-holder">
<!-- <img src="images/mainGallery/main-galery1.jpg" class="active" >
<img src="images/mainGallery/main-galery2.jpg" >
<img src="images/mainGallery/main-galery3.jpg" >
-->
</div>
Jquery
$(document).ready(function(){
$.ajax({
url: 'mainGallery.php',
success: function(data){
$('#gallery-holder').html(data);
}
}).error(function(){
alert('an alert occored');
}).success(function(){
// alert('success');
}).complete(function(){
// alert('complete');
});
slideSwitch();
});
function slideSwitch() {
var $gallery = $('#gallery-holder'),
$active = $gallery.find('img:visible'),
$next = $active.next().length ? $active.next() : $gallery.find('img').first();
setTimeout(function() {
$active.fadeOut('slow');
$next.fadeIn('slow', slideSwitch);
}, 2000);
};
PHP
<?php
$i=0;
foreach(glob('./images/mainGallery/*.*' ) as $filename){
if ($i==0){
echo '<img src="'.$filename.'" class="active">';
}
else echo '<img src="'.$filename.'">';
$i++;
}
?>
It's look like the HTML is not recognize the Active class form the AJAX.
No errors in the console.
please help...
thanks,
Cfir.
Move your function call inside the success callback, otherwise it will run before the elements have been added:
$(document).ready(function(){
$.ajax({
url: 'mainGallery.php',
success: function(data){
$('#gallery-holder').html(data);
slideSwitch(); //Initialize slider after elements are loaded into the DOM
}
);
});
Inferring from this: IE ignores styles for dynamically loaded content, I'd suggest you try and return something like <img src="images/mainGallery/main-galery1.jpg" id="displayimg"> from your PHP, and then do:
$('#gallery-holder').html(data);
$('#displayimg').addClass("active");
Try it.

Redirecting and then run function on another page?

So I have the code:
header("Location: ".$config['site_url']."index.php#login-box");
Which is my attempt to trigger the html code for a jquery login form if the user is not logged in:
<li><a class="log" href="#login-box"><span class="hover" style="opacity: 0;"></span></a></li>
But when it runs all it does is add #login-box to then end of my url and doesn't trigger the #login-box href. Pretty lame attempt I know, but any ideas?
This is the jquery script, how would I add the code you mentioned to it properly?
$(document).ready(function() {
$('a.login-window2').click(function() {
//Getting the variable's value from a link
var loginBox = $(this).attr('href');
//Fade in the Popup
$(loginBox).fadeIn(300);
//Set the center alignment padding + border see css style
var popMargTop = ($(loginBox).height() + 24) / 2;
var popMargLeft = ($(loginBox).width() + 24) / 2;
$(loginBox).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
// Add the mask to body
$('body').append('<div id="mask"></div>');
$('#mask').fadeIn(300);
return false;
});
// When clicking on the button close or the mask layer the popup closed
$('a.close, #mask').live('click', function() {
$('#mask , .login-popup').fadeOut(300 , function() {
$('#mask').remove();
});
return false;
});
});
</script>
Here is a little snippet that should do the trick, using jQuery as you mentioned:
$(function(){
$("a.login-window2").click(function(){
//Getting the variable's value from a link
var loginBox = $(this).attr("href");
//Fade in the Popup
$(loginBox).fadeIn(300);
//Set the center alignment padding + border see css style
var popMargTop = ($(loginBox).height() + 24) / 2;
var popMargLeft = ($(loginBox).width() + 24) / 2;
$(loginBox).css({
"margin-top" : -popMargTop,
"margin-left" : -popMargLeft
});
// Add the mask to body
$("body").append("<div id=\"mask\"></div>");
$("#mask").fadeIn(300);
return(false);
});
// When clicking on the button close or the mask layer the popup closed
$("a.close, #mask").live("click", function(){
$("#mask , .login-popup").fadeOut(300, function(){
$("#mask").remove();
});
return(false);
});
// Check the hash
var hash = $(location).attr("hash");
if(hash == "#login-box") {
$("a.login-window2").click();
}
});
Put this in your index.php file and run your code from inside the if statement.
It's a mistake in your code.
Use: name="#login-box"
instead of: href="#login-box"

Colorbox fade in content smoothly

I have this problem: I use colorbox to make those jQuery popups (colorbox). The problem is that I want to fade in the content smoothly via ajax without that loading div which you can see right before the content shows up.
When I disable the loading div via opacity it is gone, but I can't fade in my content smoothly. It just pop-ups suddenly.
<script type="text/javascript">
$(document).ready(function(){
$(".register_link").colorbox({
initialWidth:'886',
initialHeight:'410',
fixed:'true',
scrolling:'false',
transition:'fade',
onOpen: function(){
$("#colorbox").css("opacity", 0);
},
onComplete: function(){
var title = 'Register';
$('#cboxTitle').text(title);
$("#colorbox").css("opacity", 1);
}
});
});
</script>
You can use the jQuery animate() function, instead of the .css() function
onComplete: function(){
var title = 'Register';
$('#cboxTitle').text(title);
$("#colorbox").animate({"opacity": 1});
}

How can I append a value from the database to an anchor tag's href attribute?

I am using a popup jquery that pops up a static block of code using a href=#?w=300?h=200 but I need apart from the size to pass a variable from a sql database also a href=?prop_id=$id . I have tried to combine both of them but nothing seems to work does anybody has a clue??
thank you
$(document).ready(function() {
//When you click on a link with class of poplight and the href starts with a #
$('a.poplight[href^=#]').click(function() {
var popID = $(this).attr('rel'); //Get Popup Name
var popURL = $(this).attr('href'); //Get Popup href to define size
//Pull Query & Variables from href URL
var query= popURL.split('?');
var dim= query[1].split('&');
var popWidth = dim[0].split('=')[1]; //Gets the first query string value
//Fade in the Popup and add close button
$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<img src="to_use/close.png" class="btn_close" title="Close Window" alt="Close" />');
//Define margin for center alignment (vertical horizontal) - we add 80px to the height/width to accomodate for the padding and border width defined in the css
var popMargTop = ($('#' + popID).height() + 80) / 2;
var popMargLeft = ($('#' + popID).width() + 80) / 2;
//Apply Margin to Popup
$('#' + popID).css({
'margin-top' : -popMargTop,
'margin-left' : -popMargLeft
});
//Fade in Background
$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies
return false;
});
//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
$('#fade , .popup_block').fadeOut(function() {
$('#fade, a.close').remove(); //fade them both out
});
return false;
});
});
A simpler approach would be to use data- attributes in combination with jQuery's .data() method
HTML
Text
JS
/* in click handler */
var data=$(this).data();
var propId=data.propId;/* same as $(this).data('propId') */
var popWidth=data.w;
/* etc..*/

Infinite scroll with jquery and php

I use this function to create an infite scrolling on my page. The thing is it displays the default div's (onload) and one batch with divs when i scroll to the bottom. But when i scroll to the bottom once again when the batch is added it won't trigger the loadmore.php function. It says: no more posts to show
What am i doing wrong?
<div id="wrapper">
<div id="postswrapper">
<div class="postitem" id="4"><img src="http://ww.supair.fr/photos_cam1_fixe/image.jpg" /></div><div class="postitem" id="5"><img src="http://www.bormioonline.com/imagesntent/webcam/webcam06-320x240right.jpg" /></div>
</div>
<script type="text/javascript">
var loading = false;
$(window).scroll(function(){
var h = $('#postswrapper').height();
var st = $(window).scrollTop();
// the following tests to check if
// 1. the scrollTop is at least at 70% of the height of the div, and
// 2. another request to the ajax is not already running and
// 3. the height of the div is at least 500.
// You may have to tweak these values to work for you.
if(st >= 0.7*h && !loading && h > 500){
loading = true;
$('div#loadmoreajaxloader').show();
$.ajax({
url: "/test2/loadmore.php?lastid=" + $(".postitem:last").attr("id"),
success: function(html){
if(html){
$("#postswrapper").append(html);
$('div#loadmoreajaxloader').hide();
}else{
$('div#loadmoreajaxloader').html('<center>No more posts to show.</center>');
}
loading = false;
}
});
}
});

Categories