I'm trying to update active/suspend button using AJAX and jQuery, after user clicked on "suspend" -> I have to change the status to "Suspended" + active button.
After user clicked on "active" button -> I change the status to "Activated" + suspend button.
Here is the HTML/PHP code :
Status :
<?php
if($row['active']==2) // 2 = Suspended
{
?>
<span id="status<?php echo $row['id']; ?>">Suspended
<button type="button" class="active-button" value="<?php echo $row['id']; ?>">active</button>
</span>
<?php
}
else if($row['active']==1) // 1 = Activated
{
?>
<span id="status<?php echo $row['id']; ?>">Activated
<button type="button" class="suspend-button" value="<?php echo $row['id']; ?>">suspend</button>
</span>
<?php
}
?>
This is the jQuery/AJAX code :
$(function() {
$('.active-button').click(function(){
var add_active = 1;
var add_id = $(this).val();
var status = "#status"+add_id;
$(status).text('Loading...');
$.post('active.php',{id: add_id, active: add_active}, function(){
$(status).html("").append("Suspended <button type='button' class='active-button' value="+add_id+">active</button>");
});
});
$('.suspend-button').click(function(){
var add_active = 2;
var add_id = $(this).val();
var status = "#status"+add_id;
$(status).text('Loading...');
$.post('active.php',{id: add_id, active: add_active}, function(){
$(status).html("").append("Activated <button type='button' class='suspend-button' value="+add_id+">suspend</button>");
});
});
});
But The problem is that the span "status" doesn't change(in the DB it works well).
I'm almost sure that the problem is in this line:
$.post('active.php',{id: add_id, active: add_active}, function(){
$(status).html("").append("Activated <button type='button' class='suspend-button' value="+add_id+">suspend</button>");
});