PHP - Store & Calculate the total mark from radio input

点点圈 提交于 2019-12-31 06:08:34

问题


I have designed a small web-based system that have a school evaluation form to ask specific users who can access the system" some questions and the input will be a radio type ( 1 or 2 or 3 or 4)! the code is working and can inserts the input into the database but i don't know the correct query to calculate the total mark and store it in the database, this is currently working code below:

    <?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>

i used this query but it doesn't work out .. any help please?

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

    ?>  

回答1:


I'm not sure I understand what you're asking. You want to store both the separate values and the sum, but you don't want to do the sum on your PHP?

If so, you could make a trigger, something like that:

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

This way, you'd only have to run the following query:

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

... and the trigger should fill the 'total' column with the sum P1 + P2 + P3.

This trigger I created will basically says: "everytime someone inserts something into 'Form1', fill the 'TOTAL' column with the sum P1 + P2 + P3".

Also, you should NOT be using mysql_query. Check out mysqli or PDO_MySQL. mysql_query is outdated, was discontinued and is unsafe.




回答2:


PHP is a server side technology, it cannot respond to changes in a page without submitting to the server, maybe with an AJAX call or something. However what it appears you need to do is set up some onclick events and use javascript to add your total up into a hidden element in your form and submit that along with the rest.

...
<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>

Edit: Corrected to account for JS treating values as strings by default.



来源:https://stackoverflow.com/questions/13312033/php-store-calculate-the-total-mark-from-radio-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!