I do have 4 text inputs like this,
<input type="text" name="amount" id="amount">
<input type="text" name="commission" id="commission">
<input type="text" name="discount" id="discount">
<input type="text" name="total_amount" id="total_amount">
Here I am calculating total_amount
base on the values of #amount
. To do this I am using ajax like this,
var minlength = 2;
$("#amount").on('input', function () {
var value = $(this).val(),
if (value.length >= minlength ) {
$.ajax({
type: "POST",
url: "includes/transactions/proccess_amount.php",
data: {'amount':value},
success: function(response) {
response = jQuery.parseJSON(response)
$('#make_transaction')
.find('[name="commission"]').val(response.commission).end()
.find('[name="total_amount"]').val(response.total_amount).end();
}
});
}
});
Now I want to change total_amount
again if it has a value on #discount
field. So I created a ajax request for #discount keyup
and its working for me.
keyup
script look like this.
var strLength = 1;
$("#discount").keyup(function () {
var discount = $(this).val(),
amount = $('#amount').val(),
comison = $('#commission').val(),
total = $('#total_amount').val();
if (discount.length >= strLength ) {
$.ajax({
type: "POST",
url: "includes/transactions/proccess_discount.php",
data: { 'amount':amount
, 'comison': comison
, 'total' : total
, 'discount': discount
},
success: function(response) {
response = jQuery.parseJSON(response)
$('#amount_dolar').val('');
$('#make_transaction')
.find('[name="amount_dolar"]').val(response.amount).end()
.find('[name="total_amount"]').val(response.totalAfterDiscount).end();
}
});
}
});
My problem is I want to display previous total_amount
when discount field is empty. At this stage it is always displaying the total with discount.
Update:
Image with the output for better understanding.
Can anybody tell how to do this? Thank you.