views:

64

answers:

1

When we change the name="quantity" and the $product['price'] value will changing too. Here will have dynamic quantity and price. How to do that using jquery/javascript.

<?php
$select  = "SELECT * FROM `products` ORDER BY `id` DESC LIMIT 10"; 
$query  = mysql_query($select) or die(mysql_error());
?>

<ul>
  <?php while($product = mysql_fetch_array($query)) { ?>
  <li>
    <p><?php echo $product['name'];?></p>
    <p> Quantity
      <select name="quantity">
        <?php  for($i=1;$i<=$product['quantity'];$i++) { ?>
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
        <?php } ?>
      </select>
   </p>
   <p><?php echo $product['price'];?></p>
 </li>
<?php } ?>
</ul>

Let me know :)

+1  A: 

Using jQuery:

$(document).ready(function() {
    $("#quantity_select").bind("change", function() {
        selected_option = $("option:selected").val();
        $("#counter").html(selected_option);
    });
});

...

<select id="quantity_select">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
</select>

<div id="counter"></div>

You can use text() instead of val() if you want to see actual text instead of value attribute .

Ondrej Slinták
It is bad practice to use JavaScript attributes embedded directly into HTMl.
Justin Johnson
Edited for greater good :)
Ondrej Slinták