I am trying to take an <option> variable so that I can use it to calculate the total price of a product.
E.g. The <option> is the quantity of an item. I need that to multiply with the price to get the total. The total will be displayed on the same page as the dropdown. How do I assign a variable to the select option?
<?php
if ($product->quantity < $product->max) $q = $product->quantity;
if ($product->quantity > $product->max) $q = $prodcut->max;
if ($product->quantity = $product->max) $q = $product->max;
for($i=1; $i<=$q; $i++){
echo '<option value="'.$i.'">'.$i.'</option><br/>';
}
?>
If I understand you correctly you want to be able to dynamically show a sub total of items on the page?
The best way to do this is to use javascript (jQuery). Once the page is rendered on your browser it is no longer being run server-side. PHP is a server side programming language and therefore cannot run once the HTML has been delivered to the browser.
There are two ways around this. You can use ajax (linked to jQuery’s ajax function) to send the option value via javascript to a php script which will calculate the price and send it back.
However, as its a pretty simple calculation that doesn’t require server side programming, it might be better just to do it all client side. E.g. in jQuery