0

this is my code...

$(function() {
$(".Like").livequery("click", function(e)
{               
                      
var ldl_session_id = $("#session_id").val();
var name = $("#commentName").val();    
    
var ldl_id = $(this).attr("id").replace("post_id","");
var parent  = $("#display_like_names-"+ldl_id);
// var parent = $(this);
$(this).fadeOut(200);
$.ajax({              
type: "POST",              
url: "ajax_like_names.php",
data: "ldl_post_session_id="+ldl_session_id+"&ldl_post_id="+ldl_id, 
cache: true,
success: function(html)
{
  
parent.html(html);

parent.fadeIn(200);
   
}

});

return false;

});
});



$(function() {
$(".Like").livequery("click", function(e)
{               
                      
var ldl_session_id = $("#session_id").val();
var name = $("#commentName").val();    
    
var ldl_id = $(this).attr("id").replace("post_id","");

var parent = $(this);
$(this).fadeOut(200);
$.ajax({              
type: "POST",              
url: "ajax_like.php",
data: "ldl_post_session_id="+ldl_session_id+"&ldl_post_id="+ldl_id, 
cache: true,
success: function(html)
{
// $("#display_like_names-"+ldl_id).load("posts.php #display_like_names-"+ldl_id);  
parent.html(html);

parent.fadeIn(200);
   
}

});

return false;

});
});

it works fine with bringing in the content from url ajax_like.php but i need to click twice to bring in the content from ajax_like_names.php.

i dont understand why i need to click twice to get ajax_like_names to work, i looked in firebug and the varibles are being posted on the first click but its not bringing the html back untill the second click, please help :)

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by luke noob
0

Luke,

I can't immediately see why both ajax responses should not appear unless parent in the first call and parent in the second call both address the same element, in which case it is a matter of chance as to which response arrives first and which arrives second (you may see the first response briefly, then it would be overwritten with the second). But maybe that is not the case.

Unless there's good reason for two separate "click" handlers, you can combine the functionality into one handler, either as parallel or serial ajax calls

//Parallel ajax calls

$(function() {
	$(".Like").livequery("click", function(e) {
		var ldl_session_id = $("#session_id").val();
		var name = $("#commentName").val();
		var ldl_id = $(this).attr("id").replace("post_id","");
		var data = "ldl_post_session_id="+ldl_session_id+"&ldl_post_id="+ldl_id;
		var parent1 = $("#display_like_names-"+ldl_id);
		var parent2 = $(this);
		// var parent = $(this);
		$(this).fadeOut(200);
		$.ajax({
			type: "POST",
			url: "ajax_like_names.php",
			data: data,
			cache: true,
			success: function(html) {
				parent1.html(html);
				parent1.fadeIn(200);
			}
		});
		$.ajax({
			type: "POST",
			url: "ajax_like.php",
			data: data,
			cache: true,
			success: function(html) {
				parent2.html(html);
				parent2.fadeIn(200);
			}
		});
		return false;
	});
});

//Serial ajax calls

$(function() {
	$(".Like").livequery("click", function(e) {
		var ldl_session_id = $("#session_id").val();
		var name = $("#commentName").val();
		var ldl_id = $(this).attr("id").replace("post_id","");
		var data = "ldl_post_session_id="+ldl_session_id+"&ldl_post_id="+ldl_id;
		var parent1 = $("#display_like_names-"+ldl_id);
		var parent2 = $(this);
		$(this).fadeOut(200);
		$.ajax({
			type: "POST",
			url: "ajax_like_names.php",
			data: data,
			cache: true,
			success: function(html) {
				parent1.html(html);
				parent1.fadeIn(200);
				$.ajax({
					type: "POST",
					url: "ajax_like.php",
					data: data,
					cache: true,
					success: function(html) {
						parent2.html(html);
						parent2.fadeIn(200);
					}
				});
			}
		});
		return false;
	});
});

Both should work as long as parent1 and parent2 don't address the same element.

If they don't work, then the serial form may be a useful diagnostic tool in that it should give you more of a chance of seeing what's going on - first and second responses will be further apart in time - and the order in which they arrive will be guaranteed to be the same as the order in which the calls were made.

Airshow

Edited by Airshow: n/a

0

Luke,

I can't immediately see why both ajax responses should not appear unless parent in the first call and parent in the second call both address the same element, in which case it is a matter of chance as to which response arrives first and which arrives second (you may see the first response briefly, then it would be overwritten with the second). But maybe that is not the case.

Unless there's good reason for two separate "click" handlers, you can combine the functionality into one handler, either as parallel or serial ajax calls

//Parallel ajax calls

$(function() {
	$(".Like").livequery("click", function(e) {
		var ldl_session_id = $("#session_id").val();
		var name = $("#commentName").val();
		var ldl_id = $(this).attr("id").replace("post_id","");
		var data = "ldl_post_session_id="+ldl_session_id+"&ldl_post_id="+ldl_id;
		var parent1 = $("#display_like_names-"+ldl_id);
		var parent2 = $(this);
		// var parent = $(this);
		$(this).fadeOut(200);
		$.ajax({
			type: "POST",
			url: "ajax_like_names.php",
			data: data,
			cache: true,
			success: function(html) {
				parent1.html(html);
				parent1.fadeIn(200);
			}
		});
		$.ajax({
			type: "POST",
			url: "ajax_like.php",
			data: data,
			cache: true,
			success: function(html) {
				parent2.html(html);
				parent2.fadeIn(200);
			}
		});
		return false;
	});
});

//Serial ajax calls

$(function() {
	$(".Like").livequery("click", function(e) {
		var ldl_session_id = $("#session_id").val();
		var name = $("#commentName").val();
		var ldl_id = $(this).attr("id").replace("post_id","");
		var data = "ldl_post_session_id="+ldl_session_id+"&ldl_post_id="+ldl_id;
		var parent1 = $("#display_like_names-"+ldl_id);
		var parent2 = $(this);
		$(this).fadeOut(200);
		$.ajax({
			type: "POST",
			url: "ajax_like_names.php",
			data: data,
			cache: true,
			success: function(html) {
				parent1.html(html);
				parent1.fadeIn(200);
				$.ajax({
					type: "POST",
					url: "ajax_like.php",
					data: data,
					cache: true,
					success: function(html) {
						parent2.html(html);
						parent2.fadeIn(200);
					}
				});
			}
		});
		return false;
	});
});

Both should work as long as parent1 and parent2 don't address the same element.

If they don't work, then the serial form may be a useful diagnostic tool in that it should give you more of a chance of seeing what's going on - first and second responses will be further apart in time - and the order in which they arrive will be guaranteed to be the same as the order in which the calls were made.

Airshow

thankyou it worked perfectly :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.