2012-11-09 19 views
0

我已經使用此代碼來計算v1,v2,v3的總和(總數),wihch是一個無線電輸入類型,所有數據庫中的所有數據都保存在TOTAL中。它存儲像144或444!所有3個輸入不是總和..請幫助我想把它作爲存儲的3個輸入如何在數據庫中存儲總和操作

... 
<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 += arrV1[i].value; 
     } 
    } 

    for(var i=0; i<arrV2.length ; i++){ 
     if(arrV2[i].checked == true){ 
     sumRad += arrV2[i].value; 
     } 
    } 

    for(var i=0; i<arrV3.length ; i++){ 
     if(arrV3[i].checked == true){ 
     sumRad += arrV3[i].value; 
     } 
    } 

    document.getElementById('total').value = sumRad ; 
    } 
</script> 


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


</body> 
</html> 
總和

的代碼的其餘部分是

 <?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"); 
} 


?> 
+0

從你的[最後一個問題]的答案是什麼(http://stackoverflow.com/questions/13312033/php-store-calculate-the-total-mark-from-radio-input)?你爲什麼不在收到它們時將它們添加到PHP中? – mario

回答

0

您需要parseInt()sumRad += arrV1[i].value;

sumRad += parseInt(arrv1[i].value,10)

0

在JavaScript中,你實際上是在做,因爲值的字符串連接爲這些外商投資企業lds不是整數表示,而是s字符串表示。爲了讓您的總和,你應該這樣做:

sumRad += parseInt(arrV1[i].value); 

我也將與評論質疑爲什麼你甚至做求和在JavaScript同意。你只是更新一個隱藏的領域,所以你可以輕鬆地刪除隱藏的領域和所有相關的JavaScript,並簡單地在PHP中進行求和。

0

這就是你如何做一個總和:

$v1 = intval($_POST['v1']); 
$v2 = intval($_POST['v2']); 
$v3 = intval($_POST['v3']); 
$total = $v1 + $v2 + $v3; 

在Javascript中這樣做是顯示不錯,但不是太可靠的(可以被禁用,即使很少)。

這裏intval()使得用於mysql_real_escape_string()是可接受的替代您在使用過時的數據庫接口,而不是用準備statemens簡單的PDO應該使用

相關問題