【问题标题】:PHP - Store & Calculate the total mark from radio inputPHP - 存储和计算无线电输入的总分
【发布时间】:2012-10-30 00:09:36
【问题描述】:

我设计了一个基于网络的小型系统,它有一个学校评估表来询问可以访问系统的特定用户”一些问题,输入将是无线电类型(1 或 2 或 3 或 4)! 代码正在运行,可以将输入插入数据库,但我不知道正确的查询来计算总分并将其存储在数据库中,这是当前工作代码:

    <?php

   session_start();
   $Load=$_SESSION['login_user'];
   include('../connect.php');
   $sql= "Select name from student where ID='$Load'";
   $username = mysql_query($sql);
    $id=$_SESSION['login_user'];

                if (isset($_POST['submit']))

{  

   $v1 = $_POST['v1'];
   $v2 = $_POST['v2'];
   $v3 = $_POST['v3'];
   $total = $_POST['total'];

 mysql_query("INSERT into Form1 (P1,P2,P3,TOTAL)
 values('$v1','$v2','$v3','$total')") or die(mysql_error());
 header("Location: mark.php");
 }


?>


<html>

<head>

<?php


if(!isset($_SESSION['login_user']))

header("Location:index.html");



?>
  <title>Q&A Form</title>

</head>

<body>


    <center><form method="post" action=""  >

    <table style="width: 20%" >
        <tr>
    <th> Criteria </th>
    <th> </th>
    </tr>
    <tr>
    <th> Excellent </th>
    <td >  4 </td>
    </tr>
    <tr>
    <th > Good <font size="3" > </font></th>
    <td>  3 <font size="4" > </font></td>
    </tr>
    <tr>
    <th > Average <font size="3" > </font></th>
    <td >  2 <font size="4" > </font></td>
    </tr>
    <tr>
    <th > Poor <font size="3" > </font></th>
    <td >  1 <font size="4" > </td>
    </tr>




<font size='4'>
    <table style="width: 70%">
        <tr>
<th > School Evaluation <font size="4" > </font></th>

<tr>
<th > Criteria <font size="4" > </font></th>
            <th> 4<font size="4" > </font></th>
            <th> 3<font size="4" > </font></th>
            <th> 2<font size="4" > </font></th>
            <th> 1<font size="4" > </font></th>

        </tr>
<tr>
<th> Your attendance<font size="4" > </font></th>
<td>  <input type="radio" name ="v1" value = "4"    checked = "checked" /></td>
<td>  <input type="radio" name ="v1" value = "3"     /></td>
<td>  <input type="radio" name ="v1" value = "2"     /></td>
<td>  <input type="radio" name ="v1" value = "1"     /></td>    
</tr>

<tr>
<th > Your grades  <font size="4" > </font></th>
<td>  <input type="radio" name ="v2" value = "4"    checked = "checked" /></td>
<td>  <input type="radio" name ="v2" value = "3"     /></td>
<td>  <input type="radio" name ="v2" value = "2"     /></td>
<td>  <input type="radio" name ="v2" value = "1"     /></td>    
</tr>

<tr>
<th >Your self-control <font size="4" > </font></th>
<td>  <input type="radio" name ="v3" value = "4"    checked = "checked" /></td>
<td>  <input type="radio" name ="v3" value = "3"     /></td>
<td>  <input type="radio" name ="v3" value = "2"     /></td>
<td>  <input type="radio" name ="v3" value = "1"     /></td>    
</tr>       


        </tr>
    </table>


    <br>
    <a href="evaE.php">  <td><input type="submit" name="submit" value="Submit">

    <input type="reset" name="clear" value="clear" style="width: 70px"></td>
<?php
$total = $v1+ $v2 + $v3;

?>  


 </form> 
</center>
</div>


</body>
</html>

我使用了这个查询,但它不起作用..请帮忙?

 <?php
    $total = $v1+ $v2 + $v3;

    ?>  

【问题讨论】:

    标签: php radio


    【解决方案1】:

    我不确定我是否明白你在问什么。您想存储单独的值和总和,但不想在 PHP 上进行总和?

    如果是这样,您可以创建一个trigger,类似这样:

    DELIMITER $$
    CREATE TRIGGER `sum_values` BEFORE INSERT
        ON `Form1`
        FOR EACH ROW
    BEGIN
        SET NEW.TOTAL = NEW.P1 + NEW.P2 + NEW.P3;
    END$$
    DELIMITER ;
    

    这样,您只需运行以下查询:

    INSERT INTO 
        Form1 
            (P1,P2,P3)
        VALUES
            ('$v1','$v2','$v3')
    

    ...触发器应该用总和 P1 + P2 + P3 填充“总计”列。

    我创建的这个触发器基本上会说:“每次有人在'Form1'中插入一些东西,用总和P1 + P2 + P3填充'TOTAL'列”。

    另外,你不应该使用 mysql_query。查看 mysqli 或 PDO_MySQL。 mysql_query 已过时、已停用且不安全。

    【讨论】:

      【解决方案2】:

      PHP 是一种服务器端技术,如果不提交到服务器,它可能无法响应页面中的更改,可能是通过 AJAX 调用或其他方式。但是,您似乎需要做的是设置一些 onclick 事件并使用 javascript 将您的总数添加到表单中的隐藏元素中,然后将其与其余部分一起提交。

      ...
      <tr>
      <th> Your attendance<font size="4" > </font></th>
      <td>  <input type="radio" name ="v1" value = "4"    checked = "checked" onclick="updateTotal();"/></td>
      <td>  <input type="radio" name ="v1" value = "3"  onclick="updateTotal();"   /></td>
      <td>  <input type="radio" name ="v1" value = "2"  onclick="updateTotal();"   /></td>
      <td>  <input type="radio" name ="v1" value = "1"  onclick="updateTotal();"   /></td>    
      </tr>
      
      <tr>
      <th > Your grades  <font size="4" > </font></th>
      <td>  <input type="radio" name ="v2" value = "4"  onclick="updateTotal();"  checked = "checked" /></td>
      <td>  <input type="radio" name ="v2" value = "3"  onclick="updateTotal();"   /></td>
      <td>  <input type="radio" name ="v2" value = "2"  onclick="updateTotal();"   /></td>
      <td>  <input type="radio" name ="v2" value = "1"  onclick="updateTotal();"   /></td>    
      </tr>
      
      <tr>
      <th >Your self-control <font size="4" > </font></th>
      <td>  <input type="radio" name ="v3" value = "4"  onclick="updateTotal();"  checked = "checked" /></td>
      <td>  <input type="radio" name ="v3" value = "3"  onclick="updateTotal();"   /></td>
      <td>  <input type="radio" name ="v3" value = "2"  onclick="updateTotal();"   /></td>
      <td>  <input type="radio" name ="v3" value = "1"  onclick="updateTotal();"   /></td>    
      </tr>       
      
      
              </tr>
          </table>
      
      
          <br>
          <a href="evaE.php">  <td><input type="submit" name="submit" value="Submit">
      
          <input type="reset" name="clear" value="clear" style="width: 70px"></td>
      
          <input type="hidden" id="total" name="total" />
      
      
      <script>
        function updateTotal(){
      
          var sumRad = 0;
      
          var arrV1 = document.getElementsByName("v1");
          var arrV2 = document.getElementsByName("v2");
          var arrV3 = document.getElementsByName("v3");
      
          for(var i=0; i<arrV1.length ; i++){
            if(arrV1[i].checked == true){
              sumRad += parseInt(arrV1[i].value);
            }
          }
      
          for(var i=0; i<arrV2.length ; i++){
            if(arrV2[i].checked == true){
              sumRad += parseInt(arrV2[i].value);
            }
          }
      
          for(var i=0; i<arrV3.length ; i++){
            if(arrV3[i].checked == true){
              sumRad += parseInt(arrV3[i].value);
            }
          }
      
          document.getElementById('total').value = sumRad ;
        }
      </script>
      
      
       </form> 
      </center>
      </div>
      
      
      </body>
      </html>
      

      编辑:更正以说明 JS 默认将值视为字符串。

      【讨论】:

      • 我用过你的代码,它把总数存储在数据库中,比如 1232444444 .. 不做总和!
      • @user1806136 - 默认情况下,JS 将值解析为字符串,我很傻,忘记了,将每组收音机的 sumRad += arrV1[i].value; 更改为 sumRad += parseInt(arrV1[i].value);,你应该很高兴。
      猜你喜欢
      • 2022-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      相关资源
      最近更新 更多