using jquery/ajax to send/update div when link is clicked - php

I have 2 layers that i need to update individually if a user clicks on either of them.
<div id=wrapper>
<div id=upvote>
//This div can be filled by upvote.php?id=X
<? echo getUpVote(); ?>
<a href=#><img src=upv.png></a>
</div>
<div id=downvote>
//This div can be filled by downvote.php?id=X
<? echo getdownVote(); ?>
<a href=#><img src=downv.png></a>
</div>
</div>
When the user clicks the up or down vote image, i need to fade out the contents of the div, make an ajax call to the respective php file (get request), and fade in the loaded content.
How can i do this?

Attach a click handler to the anchor tags. Find which url to use given the id of the containing div, then invoke load to replace the contents of the wrapper. Fade out/in at the appropriate points. Make sure that you return false from the anchor click handler to cancel the default action.
$('#upvote,#downvote').find('a').click( function() {
var url = $(this).closest('div').attr('id') + '.php?id=X';
$('#wrapper').fadeOut();
.load( url, function() {
$('#wrapper').fadeIn();
});
return false; // cancel click action
});

To add a click handler to an element in jQuery you can do this:
$("#upvote").click(clickHandler);
Where clickHandler is a function. Since you want to fade Out an image you can do this:
$("#upimg").hide('slow');
will cause an element with id='upimg' to disappear slowly. You can also use fadeTo(opacity) to achieve a similar effect.
The AJAX call can be made with a call to load.
$('#div').load('url', {}, callback);
where an element with id='div' will be populated with the result of calling url, the {} is optional and callback is a function that you can include to execute after the load. The callback function may be used to fadeIn the new content.
var clickHandler = function(){
$("#upimg").hide('slow');
$('#div').load('url', {}, callback);
}
var callback = function(){
$('#div').show('slow');
}

AJAX request can be made with $.get and you can fade the DIV using jQuery's animation functions like fadeOut
Here is some code on how you might acchive what you want:
$( '#IdOfOneAnchor' ).click ( function () {
var myDiv = $( '#IdOfYourDiv' );
myDiv.fadeOut ();
$.get (
'url/to/your/script.php',
{
// put params you need here
},
function ( response ) {
myDiv.html ( response ).fadeIn ();
}
);
return false;
} );
off course you will have to change the selectors to match your DIVs/links ...

Related

How to load dynamic content (jquery) inside ajax call?

My homepage loads pages with jquery ajax call. In the Pictures subpage there is 3 div, and each loads a php with ajax, too. I would like to include a gallery js to every sub-subpage. However, the js does not loads. Here is my code in the index.php for the subpages:
<script type="text/javascript">
$(function()
{
var actualmenu = 'fooldal.html';
$('#tartalom').load('fooldal.html');
$('.fooldal').click(function()
{
$('#tartalom').load('fooldal.html');
actualmenu = 'fooldal.html';
}
);
$('.kepek').click(function(){
$('#tartalom').load('kepek.php', function(){
$(document.body).on('click', 'a[rel=gallery_view]' ,function(){
$(this).KeViewer('gallery', {'percent' : 70});
return false;
});
});
actualmenu = 'kepek.php';
});
});
</script>
And there is my kepek.php page:
<script type="text/javascript">
$(function()
{
$('#galeria').load('kepek_csenge.php');
$('#csenge').click(function()
{
$('#galeria').load('kepek_csenge.php');
}
);
);
</script>
<div id="albums">
<div class="album" id="csenge">
Csenge
<br/>
<img src="albumok/csenge/01.jpg" alt="csenge"/>
</div>
</div>
<div style="clear:both;"></div>
<div id="galeria" width="500"></div>
Inside kepek_csenge.php there are rows like this, which should trigger the gallery:
<a class="thumb_wrapper" href="albumok/csenge/01.jpg" title="Első" rel="gallery_view"><img alt="" class="thumb" src="albumok/csenge/thumbs/01.jpg" /></a>
Unfortunately it just loads a new page with the selected picture. How can i use the galleryviewer js in this page?
It is important to understand that the document you are loading into was ready long before you load anything with ajax
This means that the code wrapped in $(function() in your ajax pages will fire as soon as it is encountered. If that code precedes the html it references then it won't find those elements since they don't exist yet.
Try moving all the script tags in the ajax loaded content to after the html.
It is generally easier to put all the scripts into one and wrap different parts in functions and then just call those functions in the success callback of load();
function loadPage(page){
var url = '...' +page;
$('#selector').load(url, function(){
initViewer();
});
}

refresh div with new data as if it was a page refresh

Is it possible using jQuery to literally refresh a div?
Nothing like submitting a form or anything like that.
I have a data stream which is updated else where and all I want to do is refresh the div and all its contents as if it were a page refresh. I can't link to that page to make a return that populates as the only output is just raw data.
The div itself contains all the data display processing. Nothing needs to be fetched as the data is already there.
you have to use setinterval with ajax function,
$(document).ready(function(){
setInterval(function(){ refreshDiv(); }, someInterval);
});
function refreshDiv(){
$.ajax({
url: "http://yourrequestpath",
.....
});
}
<div id="data"></div>
<script>
$('#div').load("loaddata.php", function() {
window.setInterval("loadData", 60000);
});
function loadData()
{
$('#div').load("loaddata.php");
}
</script>

How can I use jQuery effects on Ajax loaded content?

Hi and thanks for taking some time to look at my question. I have a part of the page where content is dynamicly loaded into from another file. Reason for this is it needs to be live updated. Now I want to be able to apply jquery effects that are usually used for show/hiding content (slide, fade etc) to animate the difference between the current data and the new data. This is the code used to get the content and load it into the div:
function k() {
$.post("../includes/ajaxAgenda.php", {
limit : value
}, function(data) {
$('#tab-agenda').html(data);
});
};
$(document).ready(function() {
k();
$('#tab-agenda').scroll(function() {
loadMore();
});
});
var refreshId = setInterval(function() {
k();
}, 1000);
So I guess my question is how do I animate what gets loaded in so it doesn't just "pop" from one content to another?
edit: I tried using the .live instead of .scroll but it doesn't seem to work:
$(document).ready(function() {
$('#tab-agenda').live("scroll",function() {
alert("hi");
loadMore();
});
});
You need to use live function of jquery to bind the dynamically added elements.
Ref: http://api.jquery.com/live/
Try this :
$('#tab-agenda').live("scroll",function() {
loadMore();
});
I suggest you to add the ajax loader image with proper css over the content/ div as like below.
function loadmore(){
$("#loader").css('display','block');
//your
//code
//here
$("#loader").css('display','none');
}
html
<img id="loader" src="ajax-loader.gif" style="display:none" />
<div id="content">
your cont to display
</div>

call jquery function wordpress

Below code is jquery function
$(document).ready(function () {
$('#access').accordion({
create: function (event, ui) {
$('div#access> div').each(function () {
var id = $(this).attr('id');
$(this).load(id+'.html', function () {
$('#access').accordion('resize');
});
});
}
});
});
where access is the div id i have used for menu items in header.
below code is header.php page code
<div id="access" >
<ul>
<?php wp_list_pages('depth=1&title_li=');?>
</ul>
</div>
what i want is onclick of the menu item the javascript function should be called..
how to call function from php file?
You don't call the JavaScript function from PHP. The PHP merely enables you to build the HTML page dynamically. Once the page is ready, the JavaScript is called and it starts binding the events to the appropriate elements.
What you need to do is look at the generated code using the 'view source' or firebug and explore the structure of the generated HTML and then you can bind the event to the requested element.

How to call function based on scroll bar click?

I am having the Div with scroll bar.When User clicks the scroll bar I need to call the function based on the scroll bar click.How can I make a call?
I am using Jquery in the client and using the PHP in the server side.
I know how to make ajax calls and etc.Only think is I need to make this call when scroll bar is clicked in that div.
Is it possible to make the ID for the scroll bar in that div.
You can bind to the scroll event, it's not fired when the user clicks the scroll-bar but it is fired when the scroll-bar moves for any reason:
//wait for document.ready to fire, meaning the DOM is ready to be manipulated
$(function () {
//bind an event handler to the `scroll` event for your div element
$('#my-scroll-div').on('scroll.test-scroll', function () {
//you can do your AJAX call in here, I would set a flag to only allow it to run once, because the `scroll` event fires A LOT when scrolling occurs
});
});
Note that .on() is new in jQuery 1.7 and in this case is the same as using .bind().
To set a flag like I suggest above:
$(function () {
var AJAX_flag = true;
$('#my-scroll-div').on('scroll.test-scroll', function () {
if (AJAX_flag === true) {
AJAX_flag = false;
$(this).off('scroll.test-scroll');
$.ajax({...});
}
});
});
Update
The best solution to adding event handlers to dynamically created elements is to bind to them before adding them to the DOM:
function scroll_func () {
var AJAX_flag = true;
$(this).on('scroll.test-scroll', function () {
if (AJAX_flag === true) {
AJAX_flag = false;
$(this).off('scroll.test-scroll');//unbind the event handler since we're done with it
$.ajax({...});
}
});
}
$.ajax({
...
success : function (data) {
//note that this selector will have to change to find the proper elements for you, if you are unsure how to select, start by seeing what data is by doing a `console.log(data);`
$(data).find('#my-scroll-div').on('scroll', scroll_func).appendTo('#container-element');
}
});

Categories