Here is the JavaScript I am using to handle an ajax request:
$(document).ready(function() {
// Variable to hold original content
var original_content_qty = '';
$('.product-qty-<?php echo $products->fields['
products_id ']; ?>').submit(function(e) {
e.preventDefault();
$.ajax({
url: 'submit.php',
type: 'POST',
data: $(this).serialize(),
dataType: 'html'
}).done(function(data) {
original_content_qty = $('.qty-update-<?php echo $products->fields['
products_id ']; ?>').html();
console.log(original_content_qty);
console.log(data);
$('.qty-update-<?php echo $products->fields['
products_id ']; ?>').fadeOut('slow', function() {
$('.qty-update-<?php echo $products->fields['
products_id ']; ?>').fadeIn('slow').html(data); //display a success message sent back via ajax
$('.qty-update-<?php echo $products->fields['
products_id ']; ?>').delay(1200).fadeOut('slow').html(data);
$('.qty-update-<?php echo $products->fields['
products_id ']; ?>').html(original_content_qty); // restore original content after success message
});
}).fail(function() {
alert('Ajax Submit Failed ...');
});
});
});
Issue with restoring content:
When I try to restore the original content after displaying a success message, the content appears to display, fade out, then replace the original content. The success message displays correctly without trying to restore the content.
What's causing this behavior?
Console log for the data returned from ajax:
<style>.block .notice.invalid{display:none;}</style>
<div class="alert alert-info">
<strong>Stock updated</strong>
</div>
The data returned remains consistent regardless of attempting to restore content or not.
Console log for the original_content_qty shows:
Stock: <input type="hidden" name="products_id" value="289"><input type="text" name="products-quantity" value="0" size="4" class="product_quantity_input_289">
Could there be an error in how I'm trying to restore the content?