I am using laravel framework and trying to create a conditional calculation form. I have 2 inputs for adult numbers and children numbers
<input type="text" name="nAdults" id="nAdults" value="" class="form-control ">
<input type="text" name="nChildren" id="nChildren" value="" class="form-control ">
Also I have 3 inputs where adult price, children price and total price needs to be shown. And finally there is one input to apply a discount.
<input type="text" name="nPriceAdult" id="nPriceAdult" value="" class="form-control ">
<input type="text" name="nPriceChild" id="nPriceChild" value="" class="form-control ">
<input type="text" name="nTotalPrice" id="nTotalPrice" value="" class="form-control ">
<input type="text" name="nDiscount_percent" id="nDiscount_percent" value="" class="form-control ">
My Controller file looks like this
public function priceCalculator(Request $request)
{
$nAdults = $request->input('nAdults');
$nChildren = $request->input('nChildren');
$kfnTourID = $request->input('_kfnTourID');
$nPriceAdult = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceAdult');
$nPriceChild = DB::table('tours')->where('_kpnID', $kfnTourID)->value('nPriceChild');
echo $msg = $nPriceAdult * $nAdults; }
My Blade file looks like this
var $nAdults = $('#nAdults'), $nPriceAdult = $('#nPriceAdult'), $nPriceChild = $('#nPriceChild');
$nAdults.on('keyup', function(){
$.ajax({
type: 'POST',
data: $('#bookingsFormAjax').serialize(),
url:'bookings/calculate',
dataType: 'json',
success: function(msg){
$nPriceAdult.val(msg);
$nPriceChild.val(msg);
console.log(msg);
}
});
});
I get these prices from a database
------------------------------------
_kpnID nPriceAdult nPriceChild
------------------------------------
2 100 50
3 200 100
When I type 1 in the adults field it shows 100 when I type 2 it shows 200 in adult price input. But I am not sure how to show different price in children price field and how to show the the total in the total price text input nor apply a discount by entering a rebate percent in the discount field. For example if I type "10" in discount field it should show $90 in adult price as it will apply 10% discount. I am new to programming. I would appreciate any help.