help with an add script.

Discussion in 'PHP' started by co.ador, Feb 9, 2010.

  1. #1
    I looking to sum three values of the price field if selected.
    
    $sql = "SELECT * FROM shoes
        WHERE tray on tray.product_id = shoes.id";
     $sth = mysql_query($sql);
     $variety = mysql_fetch_array($sth)) {
    echo'<div>
    <p>'. $variety['name']. '</p>
    <p class="price">'. $variety['price']. '</p>
    <input name="price" type="checkbox" value=""  />
    </div>
    <div>
    <p>'. $variety['name']. '</p>
    <p class="price">'. $variety['price']. '</p>
    <input name="price" type="checkbox" value=""  />
    </div>
    <div>
    <p>'. $variety['name']. '</p>
    <p class="price">'. $variety['price']. '</p>
    <input name="price" type="checkbox" value=""  />
    </div>
    }
    PHP:

    if you notice each iteration will have a checkbox input and users can choose one or the three if they one. Now how can I sum how if user chooses more than one price?
     
    co.ador, Feb 9, 2010 IP
  2. Brandon.Add.On

    Brandon.Add.On Peon

    Messages:
    178
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This would best be accomplished in javascript. You can add the onClick event to the checkbox and put an add function in it.

    Add the following in the checkbox input field and create the javascript function addprices to get the values of all items checked and add it then you can either return the total value in another input field or a DIV.

    
    onClick="addprices();"
    
    Code (markup):
    Each 'name' identifier of the checkbox needs to be different.

    Or if you choose to do it in php (this would require a page refresh or new page) you can simple do it like this

    
    $totalprice = $_POST['price1'] + $_POST['price2'] + $_POST['price3'];
    
    Code (markup):
     
    Brandon.Add.On, Feb 10, 2010 IP