Reputation: 433
I am trying to change the price based on the option a user choose. I am getting the price and sizes from the database. Is they a way to use ajax to change the price base on the bsize that user choose from the drop down options. In my database I have
Table name: size
Sid bsize. Sprice 1 12 200 2 14 250 3 16 400
<div class="soat_r">
<form id="add2cart" name="cart" method="Post" action="<?php fetchdir($bpages); ?>cart.php">
<table>
<tr>
<td width="160">Price:</td>
<td name="price"></td><!-- the price will go here and will change depend on the bsize the choose -->
</tr>
<tr>
<td for="size" width="160">size*:</td>
<td>
<select name="size" class="small" id ="size">
<!-- I will echo out the bsize here and it will be in a drop down <option> -->
</select>
</td>
</tr>
<tr>
</tr>
<tr>
<td>Quantity</td>
<td><input type="text" name="Qty" value="1" style="width: 20px; text-align: right" /></td>
</tr>
</table>
<div class="cleaner h20"></div>
<input type="hidden" name="pid" id="id" value="<?php echo $id; ?>" />
<input type="Submit" class="button" name="button" id="button" value="Add to shopping cart"/>
</form>
</div>
They product is listing in a differed t table so I have 1 Adidas blue 2. addias white 3. addidias green then on a different table I have the price... The price for addidas change depending on the size... Thats why I have a table for size and price... On the cart page am posting the name, the size the choose and the price(which depends on the size)... Let's say you go on the blue addias page. You choose the size, I want it to tell you the price for that size on the price tr... Without you having to add to the cart to find out... I tried to do this but I am out of ideas and I really will be greatful if someone can show me a few codes for this... Thanks
Upvotes: 2
Views: 1672
Reputation:
Sure,
if you use jQuery:
$('#size').change(function() {
$.post("get_price_by_bsize.php", "bsize="+$("#size").val())
$("#price_td").html(data);
});
});
on the get_price_by_bsize.php just run a query:
SELECT Sprice from size WHERE bsize={$_POST['bsize']}
and print the price. Don't forget to assign an id to price <td>
.
Without jQuery , same principal, but with xmlhttprequest and onclick
Upvotes: 1