0

I want to update two divs with one ajax request only. However following code clears out both the divisions without updating the data. Please have a look at my code:

AJAX Request File

<div class='subtotal_<?php echo $row['productid']; ?>'>
    <p>Subtotal: <?php echo $subtotal; ?></p>
</div>
<div id='totalamount'>
    <p>Totalamount: <?php echo round($total); ?></p>
</div>

var productid = $(this).attr('class');
$.ajax
    ({
        type: "POST",
        url: "ajax_changequantity.php",
        data: {type:type,quantity:quantity,productid:productid},
        success: function(option)
        {
            var html = $(option);
            $('#totalamount').empty();
            $('.subtotal_'+productid).replaceWith(("#subtotal",html));
            $('#totalamount').replaceWith(("#totalamount",html));
        }
    });

Ajax File

<div id='subtotal'>
    <p>Subtotal: <?php echo $subtotal; ?></p>
</div>
<div id='totalamount'>
    <p>Total Amount: <?php echo $totalamount; ?></p>
</div>

Both the divs after successful ajax request get blank. No data coming in. Can anyone help finding the mistake here?

Guy in the chair
  • 1,045
  • 1
  • 13
  • 42

1 Answers1

0

I created a sample snippet to make GET requests and update multiple elements at once. You can modify the same to make POST requests. Hope this helps.

$.when(
    $.ajax({
            type: "GET",
            url: "http://graph.facebook.com/?id=http://stackoverflow.com/",
            success: function(option){
                //do stuff
            }
        })).then(function( option ) {
        //update html
        $('.div1').html('URL: ' + option.id);
        $('.div2').html('Shares: ' + option.shares);
});

JS Fiddle

Rajender Joshi
  • 4,155
  • 1
  • 23
  • 39