【问题标题】:is it possible to add these up as a grand total using this code?是否可以使用此代码将这些加起来作为总计?
【发布时间】:2025-11-28 22:25:01
【问题描述】:

是否可以使用此代码计算总计。我希望 compcase 和 caselight 在按下按钮后给出总计。任何帮助将不胜感激,因为我无法找到一种可能的计算方式。

index.php

  function casePrice(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
} 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","caseprice.php?q="+str,true);
xmlhttp.send();
}

  function caselightPrice(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
} 
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","caselightprice.php?q="+str,true);
xmlhttp.send();
}

</script>

<Form name ="pc" Method ="Post" ACTION ="radiobutton.php">
<Input type = 'Radio' Name ='compcase' onchange="casePrice(this.value)" value= '1' />NZXT Phantom Enthusiast USB3.0 Full Tower Case - White <br />
<Input type = 'Radio' Name ='compcase' onchange="showPrice(this.value)" value= '2' />Corsair Obsidian 750D Large Tower Case Black <br />
<Input type = 'Radio' Name ='compcase' onchange="showPrice(this.value)" value= '3' />Cooler Master CM Storm Trooper Black Full Tower Gaming Case <br /><br />

<Input type = 'Radio' Name ='caselight' onchange="caselightPrice(this.value)" value= '1' />Red<br />
<Input type = 'Radio' Name ='caselight' onchange="caselightPrice(this.value)" value= '2' />Green <br /><br />

<div id="txtHint"><b>Show price here</b></div>

caseprice.php

<?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','root','','test');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"compcase");
$sql="SELECT * FROM compcase WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);

while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['case_price'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

【问题讨论】:

    标签: javascript php mysql ajax


    【解决方案1】:

    您想要案件总数吗? 如果是这样,请像这样进行选择查询:

    SELECT SUM('case_price') AS grand_total FROM compcase WHERE id = '".$q."'";
    

    然后当你通过 foreach 循环时,使用 $row['grand_total']。

    【讨论】:

    • 我希望 case_price 和 caselight_price 加起来作为一个总计。这两个单选按钮: NZXT Phantom Enthusiast USB3.0 全塔式机箱 - 白色
      红色