[英]How to calculate a total by taking the radio button values using PHP and Javascript?
I represent the questions from the database using php.我使用php代表数据库中的问题。 Using an array I display them in a table.我使用数组将它们显示在表格中。 The radio buttons are grouped by giving a variable "i" in php as the name.单选按钮通过在 php 中给出一个变量“i”作为名称来分组。 Because of that I cannot get the values of those radio buttons to a javascript function to calculate the total.因此,我无法将这些单选按钮的值传递给 javascript 函数来计算总数。
This is how the table is represented:这是表的表示方式:
I want to calculate the total from the selected radio button values and send the value to another page.我想根据选定的单选按钮值计算总数并将该值发送到另一个页面。 How do I do that?我怎么做? Thanks a lot in advance!非常感谢!
Below is my code.下面是我的代码。
<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'/>
First of all , it will better to name your inputs with a meaningful names.首先,最好用有意义的名称命名您的输入。
For example例如
echo '<td width=100> <input type="radio" name="score['.$i.']" value="1" > </td>'
this will help you to distinguish between other fields - if you need to add another fields -这将帮助您区分其他字段 - 如果您需要添加其他字段 -
in server side , you are now have a array of inputs called score
, you can sum/calculate the total with array_sum
just like :在服务器端,您现在有一个名为score
的输入数组,您可以使用array_sum
求和/计算总数,就像:
if (isset($_POST['score'])) {
$total = array_sum($_POST['score']);
}
to save this value , you can use session
with this要保存此值,您可以使用session
<input type="radio" name="score" value="1" >
, <input type="radio" name="score" value="2" >
.您可以使用相同的单选按钮但不同的值<input type="radio" name="score" value="1" >
, <input type="radio" name="score" value="2" >
。<input type="radio" class="cstm-radio" name="score" value="1" >
;在单选按钮中传递这样的自定义类<input type="radio" class="cstm-radio" name="score" value="1" >
;You can use this code to perform ajax request!您可以使用此代码执行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.