I have an order form. If the user change the packages, the price will be changed.
HTML
<select name="item" id="item">
<option value="i1">Item1</option>
<option value="i2">Item2<option>
</select>
<select name="detail" id="detail">
<option value="d1">Detail1</option>
<option value="d2">Detail2</option>
</select>
<p>Price : <span id="price"></span></p>
The PHP below will send the data gotten from the form
PHP
$item=$_POST["item"];
$detail=$_POST["detail"];
$price=0;
if($item=="p1"){
$price=$price+10;
}
else{
$price=$price+5;
}
if($detail=="d1"){
$price=$price+2;
}
else{
$price=$price+1;
}
// codes for sending the data to database
jQuery below will show the price of the chosen package. I want it to show the price everytime it's changed
jQuery
var item=$("#item").val();
var detail=$("#detail").val();
var price=0;
$("#item").on('change',function(){
if(item=="i1"){
price=price+10;
$("#price").html("$ "+price);}
else{
price=price+5;
$("#price").html("$ "+price);}
})
$("#detail").on('change',function(){
if(detail=="d1"){
price=price+2;
$("#price").html("$ "+price);}
else {
price=price+1;
$("#price").html("$ "+price);}
})
I want : price=price(item)+price(detail). The problem is, if the user change it more than once, it will add the number, althought the PHP will not send the number from jQuery.
Let's say, a user chooses Item1 and Detail2. The price shows 11. But, if the user changes it to Detail1, it will shows 13, etc, and finally, the user chooses Item2 and Detail1. PHP will send 7, but the jQuery will show more than 7. Any idea?
var item=$("#item").val(); var detail=$("#detail").val(); var price=0;into inside the on change function