ajax fails on second attempt
ive got the following bit of code
it works perfectly fine when i click the anchor once, but doesnt on the second try. on the second try it does execute the jquery on the ajax success, but it doesnt do anything to do database like it should. (refreshing the page just undo's the changes.)
<snip>
$('a.add').live("click",function(){
var squadId = $(this).attr('id');
var userId = $(this).parent().parent().parent().attr('id');
addToSquad(userId,squadId,$(this));
});
$('a.remove').live("click",function(){
var squadId = $(this).attr('id');
var userId = $(this).parent().parent().parent().attr('id');
removeFromSquad(userId,squadId,$(this));
});
function addToSquad(userId,squadId,current)
{
var dataString = "action=1&userId=" + userId + "&squadId=" + squadId;
$.ajax(
{
type: "POST",
url: "<?php echo base_url();?>admin/act",
data: dataString,
beforeSend: function()
{
},
success: function(data)
{
$(current).parent().html('<a href="#" class="remove">Remove</a> <a href="<?php echo base_url();?>admin/member/' + userId + '" class="edit">Edit</a>' + data);
}
});
return false;
}
function removeFromSquad(userId,squadId,current)
{
var dataString = "action=2&userId=" + userId + "&squadId=" + squadId;
$.ajax(
{
type: "POST",
url: "<?php echo base_url();?>admin/act",
data: dataString,
beforeSend: function()
{
},
success: function(data)
{
$(current).parent().html('<a href="#" class="add">Add</a>'+data);
}
});
return false;
}
</snip>
all this does is add or remove a user from a squad. lets say i remove the member. it works fine and on success, it replaces the html in the div that contains the remove button with <a href="#" class="add">Add</a>. if i then click on this, it should re-add the member to the squad. for some reason, this seems to fail most of the time. it doesnt apply the changes. it executes the stuff under success in the addtosquad function as if it was successfull, but when i refresh the page it still shows that member as not being a part of that squad.
there is nothing wrong with the PHP, as i can remove or add any member as long as i do one action per page refresh.
this is annoying -.-