如何乘以(或求和)从mysql数据库中检索的下拉框值

时间:2011-03-30 23:32:41

标签: php html

我被困在下拉框值计算中。有人可以帮我解决这个问题吗?

我的网页上有两个下拉框,下拉框中的值是从mysql数据库中恢复的。

下拉框A显示两个不同的值,即Chassise_NameChassise_price,下拉框B显示项目的数量。

    Dropdown box "A"                      Dropdown box "B"
SuperServer 7036A-T(Black)    $90                    3
Superserver 7036A-T(Black)检索

Chassis_Name; 从$90检索chassis_Price; 从其他表中的数量中检索3

问题:
如何才能仅在下拉框A中将价格乘以下拉框B中的数字?

这是我的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head><title></title></head>
<body>
<form action="test2.php" method="post">
<?php
mysql_connect("host", "users", "pass") or die(mysql_error());
mysql_select_db("a4202648_wang") or die(mysql_error());

echo "<table border='1'>";
echo "<tr><td>";

$result = mysql_query("SELECT Chassis_Name, Chassis_Price FROM Chassis where Chassis_Form_Factor='ATX'") 
or die(mysql_error());  
echo '<select name="Chassis" onChange="change()">'; 
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo '<option Name="Chassis">',$row['Chassis_Name'],' &nbsp;&nbsp;&nbsp;',$row['Chassis_Price'],'&nbsp;','</option>'; 
} 
echo '</select>';
echo "</td>";
echo "<td>";

$result= mysql_query("SELECT Number FROM Quantity") 
or die(mysql_error());  
echo '<select name="Quantity" Value="Quantity" onChange="change()" >'; 

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result)) {
      // Print out the contents of each row into a table
      echo '<option Name="Quantity">',$row['Number'],'</option>'; 
} 
echo '</select>';
echo "</td></tr>";
echo "</table>";
?>
<input type="submit" />
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

为了便于阅读,我减少了一些代码,但是你需要添加id =“chassis”和id =“quantity”,以便在进行求和时可以引用该dom对象。 务必将<script>放在文档的头部。

<script type="text/javascript">
    $('document').ready( function() {
        function change() {
            var quantity = $('#quantity').val();
            var chassis = $('#chassis').val();
            var sum = quantity * chassis;
            $('#the_sum').val(sum);
        }
    });
</script>
<form action="test2.php" method="post">
<?php
  echo '<select id="chassis" name="Chassis" onChange="change()">'; 

  echo '<option value="{$row['Chassis_Name']}">{$row['Chassis_Name']}&nbsp;&nbsp;&nbsp;{$row['Chassis_Price']}&nbsp;</option>'; 

  echo '<select id="quantity" name="Quantity" Value="Quantity" onChange="change()" >';
  echo '<option value="{$row['Number']}">{$row['Number']}</option>';

?>
<input type="text" value="" id="the_sum" />
<input type="submit" />
</form>
</body>
</html>