0

Hi,

I am somewhat new to PHP and MySQL and I am having a little trouble with what I want to do, which is assign a cell for each option selected and a price or insert the price into MySQL table depending on the option selected. Whichever is easier or more sufficient. Then, when you click the recalculate button, it will add up all the prices and echo the total below. Whats the best way to do this? Any help is appreciated. Here is the form:


http://illcomputers.comyr.com/custom.php


Thank You

Edited by pgmarco: clarity

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by sergb
0

You can produce the total of selected items in Javascript, without sending anything to the server or using the database for that. Of course, when the user will submit the form to purchase selected components, your PHP script will have to calculate the final price and store needed info in the database... But thats another topic.

To do this, you will need to render the price for each item in each dropdown box. You will also need to assign id attributes to each dropdown box so you can easily find them. Then, you can use javascript to get the prices of all selected items and sum them up. Whenever any of the selected items change, run the function again to get the updated total.

Here is some sample code.

<html>
<body>

Processor:<br>
<select id="pr" onchange="calc();">
    <option value="intel" price="59">Intel</option>
    <option value="amd" price="49">AMD</option>
</select>
<br>

RAM:<br>
<select id="ram" onchange="calc();">
    <option value="1G" price="20">1 GB</option>
    <option value="2G" price="40">2 GB</option>
</select>

<br><br>
Current total is <span id="total"></span>

</body>
</html>

<script type="text/javascript">
function calc()
{
    var pr = document.getElementById("pr");
    var ram = document.getElementById("ram");
    
    var pr_price = pr.options[pr.selectedIndex].getAttribute("price");
    var mem_price = ram.options[ram.selectedIndex].getAttribute("price");
    
    var total = parseInt(pr_price,10) + parseInt(mem_price,10);
    document.getElementById("total").innerHTML = total;
}

// show total with originally selected items
calc();
</script>

Hope this helps.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.