![](/img/trans.png)
[英]How to find a total sum of radio button values using jQuery/JavaScript?
[英]How to calculate a total by taking the radio button values using PHP and Javascript?
我使用php代表數據庫中的問題。 我使用數組將它們顯示在表格中。 單選按鈕通過在 php 中給出一個變量“i”作為名稱來分組。 因此,我無法將這些單選按鈕的值傳遞給 javascript 函數來計算總數。
這是表的表示方式:
我想根據選定的單選按鈕值計算總數並將該值發送到另一個頁面。 我怎么做? 非常感謝!
下面是我的代碼。
<html>
<body>
<?php
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'psych';
$connect = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
$res = mysqli_query($connect, "SELECT questions FROM questions");
$quests = mysqli_fetch_array($res);
?>
<form name="form1" id="form1" method="post" action="../../Algorithms/score.php">
<table>
<th>
<td width=100>Strongly agree </td>
<td width=100>Agree </td>
<td width=100>Neutral </td>
<td width=100>Disagree </td>
<td width=100>Strongly disagree </td>
</th>
<?php
$score=0;
$i=1;
while($row = mysqli_fetch_array($res))
{
echo "<tr> <td width=500>".$i.". ".$row['questions']."</td>";
echo '<td width=100> <input type="radio" name="'.$i.'" value="1" > </td>';
echo '<td width=100> <input type="radio" name="'.$i.'" value="2" > </td>';
echo '<td width=100> <input type="radio" name="'.$i.'" value="3" > </td>';
echo '<td width=100> <input type="radio" name="'.$i.'" value="4" > </td>';
echo '<td width=100> <input type="radio" name="'.$i.'" value="5" > </td>';
// if (score."$i") {echo "score$i";}
echo "</tr>";
// echo $scores[$i];
//$scores[$i]=$_POST['score'.$i];
$i++;
}
echo "</table>";
echo "<br>";
?>
<input type='submit' name='submit' value='Submit'/>
<input type="radio" name="score" value="1" >
, <input type="radio" name="score" value="2" >
。<input type="radio" class="cstm-radio" name="score" value="1" >
;您可以使用此代碼執行ajax請求!
$('.cstm-radio').click(function () {
var radio_val = $(this).val();
$.ajax({
//url of your php file
url: "post.php",
type: "POST",
data: {score: radio_val},
success: function (data) {
// alert radio value here
alert(data);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.