Skip to content
WJunction - Webmaster Forum

[AJAX] Need two AJAX functions to work with one button click

Status
Not open for further replies.
Hey there,

So I have this problem.
I'm making an AJAX cart for a website I'm working on.
My code is semi-functional. It gets the information correctly (Name, price, etc.), but it requests it twice so it adds it twice. So if I click on 'add to cart,' it will add it twice.

I think I know the problem but I can't really figure the solution out. Haven't worked a lot with AJAX to be honest.

This is my cart:

T2c36i4.png


Onclick, both this:

hR9oq7F.png


And this:

UTgpLl9.png


Gets updated, but like I said, unfortunately, it gets updated twice.


Here is the code for the top text:

Code:
<script type="text/javascript"><!--
$(document).ready(function () {
$('#add_to_cart').removeAttr('onclick');
$('#add_to_cart').click(function () {
		$.ajax({
			type: 'post',
			url: 'index.php?route=module/cart/minicart',
			dataType: 'html',
			data: $('#product :input'),
			success: function (html) {
				$('#textamountexel').html(html);
				}
});
});
});
</script>

Here is my code for the content inside the dropdown / white part:

Code:
<script type="text/javascript"><!--
$(document).ready(function () {
$('#add_to_cart').removeAttr('onclick');
$('#add_to_cart').click(function () {	
$.ajax({
type: 'post',
url: 'index.php?route=module/cart/callback',
dataType: 'html',
data: $('#product :input'),
success: function (html) {
$('#exelcartbody').html(html);
},
complete: function () {
var image = $('#image').offset(); 
var cart  = $('#exelcartbody').offset();
$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
params = {
top : cart.top + 'px',
left : cart.left + 'px',
opacity : 0.0,
width : $('#exelcartbody').width(),
heigth : $('#exelcartbody').height()
};
$('#temp').animate(params, 'slow', false, function () {
$('#temp').remove();
});
}
});

});
});
//--></script>

I believe it's getting added twice because I have

data: $('#product :input'),

in both codes, but I have no idea what else I can put to only grab the content and not submit it or whatever.

Help?
 

3 comments

What happens when the item is different? Does that get added "twice" as well? normally in that case i'd do is empty the container with empty().html();

for an example :

$('#exelcartbody').empty().html(function() {
$(this).append(html);
});

it'll empty the 'exelcartbody' element and append the data received from the "html" to it back. i do it for basic testing etc let me know how it goes...
 
Last edited:
u can try to do alert(html); after success function and see what value is being return, best use firbug to debug.
Also when using :input, it will get all inputs within your #product, can you post your html code inside product div?
 
Well, Baraka is a ninja. >.>

New code:
Code:
<script type="text/javascript"><!--
$(document).ready(function () {
$('#add_to_cart').removeAttr('onclick');
$('#add_to_cart').on('click',function () {	
var quantity = $("#product input[name='quantity']").val();
var product_id = $("#product input[name='product_id']").val();
$.ajax({
type: 'post',
url: 'index.php?route=module/cart/callback',
dataType: 'html',
cache: false,
data: {quantity: quantity+'&product_id='+product_id},
success: function (data) { 

$('#exelcartbody').empty().html(function() { 
$(this).append(data); 
});

var exelLoader2 = setInterval(function() {
$('#exelcartbody').load('index.php?route=module/cart/callback');
},1000);

},
complete: function () {
var image = $('#image').offset(); 
var cart  = $('#exelcartbody').offset();
$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');
params = {
top : cart.top + 'px',
left : cart.left + 'px',
opacity : 0.0,
width : $('#exelcartbody').width(),
heigth : $('#exelcartbody').height()
};
$('#temp').animate(params, 'slow', false, function () {
$('#temp').remove();
});
}
});
	return false;
});
});
//--></script>
 
Status
Not open for further replies.

About the author

E
Senior Member · Joined
I come from Uranus. Eating cookies.
2,074
Messages
543
Reactions
113
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom