<html>
<head>
</head>
<form method="post" action="data.php">
<table>
<tr>
<td>question1</td>
<td><input type="radio" name="D[1]" value="1"> 1 </td>
<td><input type="radio" name="D[1]" value="2"> 2 </td>
<td><input type="radio" name="D[1]" value="3"> 3 </td>
<td><input type="radio" name="D[1]" value="4"> 4 </td>
<td><input type="radio" name="D[1]" value="5"> 5 </td>
<td><input type="radio" name="D[1]" value="6"> 6 </td>
<td><input type="radio" name="D[1]" value="7"> 7 </td>
<td><input type="radio" name="D[1]" value="8"> 8 </td>
<td><input type="radio" name="D[1]" value="9"> 9 </td>
<td><input type="radio" name="D[1]" value="10"> 10 </td>
</tr>
<tr>
<td>question2</td>
<td><input type="radio" name="I1" value="1"> 1 </td>
<td><input type="radio" name="I[1]" value="2"> 2 </td>
<td><input type="radio" name="I[1]" value="3"> 3 </td>
<td><input type="radio" name="I[1]" value="4"> 4 </td>
<td><input type="radio" name="I[1]" value="5"> 5 </td>
<td><input type="radio" name="I[1]" value="6"> 6 </td>
<td><input type="radio" name="I[1]" value="7"> 7 </td>
<td><input type="radio" name="I[1]" value="8"> 8 </td>
<td><input type="radio" name="I[1]" value="9"> 9 </td>
<td><input type="radio" name="I[1]" value="10"> 10 </td>
</tr>
<tr>
<td>question3</td>
<td><input type="radio" name="H1" value="1"> 1 </td>
<td><input type="radio" name="H[1]" value="2"> 2 </td>
<td><input type="radio" name="H[1]" value="3"> 3 </td>
<td><input type="radio" name="H[1]" value="4"> 4 </td>
<td><input type="radio" name="H[1]" value="5"> 5 </td>
<td><input type="radio" name="H[1]" value="6"> 6 </td>
<td><input type="radio" name="H[1]" value="7"> 7 </td>
<td><input type="radio" name="H[1]" value="8"> 8 </td>
<td><input type="radio" name="H[1]" value="9"> 9 </td>
<td><input type="radio" name="H[1]" value="10"> 10 </td>
</tr>
<tr>
<td><label> question4 </label></td>
<td><input type="radio" name="A[1]" value="1"> 1 </td>
<td><input type="radio" name="A[1]" value="2"> 2 </td>
<td><input type="radio" name="A[1]" value="3"> 3 </td>
<td><input type="radio" name="A[1]" value="4"> 4 </td>
<td><input type="radio" name="A[1]" value="5"> 5 </td>
<td><input type="radio" name="A[1]" value="6"> 6 </td>
<td><input type="radio" name="A[1]" value="7"> 7 </td>
<td><input type="radio" name="A[1]" value="8"> 8 </td>
<td><input type="radio" name="A[1]" value="9"> 9 </td>
<td><input type="radio" name="A[1]" value="10"> 10 </td>
</tr><!-- 14 -->
<tr>
<td><label> question5 </label></td>
<td><input type="radio" name="D[2]" value="1"> 1 </td>
<td><input type="radio" name="D[2]" value="2"> 2 </td>
<td><input type="radio" name="D[2]" value="3"> 3 </td>
<td><input type="radio" name="D[2]" value="4"> 4 </td>
<td><input type="radio" name="D[2]" value="5"> 5 </td>
<td><input type="radio" name="D[2]" value="6"> 6 </td>
<td><input type="radio" name="D[2]" value="7"> 7 </td>
<td><input type="radio" name="D[2]" value="8"> 8 </td>
<td><input type="radio" name="D[2]" value="9"> 9 </td>
<td><input type="radio" name="D[2]" value="10"> 10 </td>
</tr><!-- 15 -->
<tr>
<tr>
<td colspan=2>
<div align="center"><input type="button" name="submit" value="Score my test" onclick="javascript:tally()"></div>
</td>
</tr>
</table>
</form>
</html>
我的意思是当用户通过无线电检查值并存储该值时,以所有名称开始=&#34; D&#34; (D1,D2,D3-D25)如何制作如此多的D值加上并存储会话??
我正在制作测验网页并且用户检查了该值(例如D1 = 5,D2 = 10)我如何加上那些并将其存储到会话中?我一直在使用java for循环,但我不能将这些var传递给php。感谢。
答案 0 :(得分:0)
你可以做一个简单的+=
赋值运算符。我把它变成了一个函数,但它可以在你的循环中完成。
编辑并添加回显到表单
我假设这个页面正在加载自己,否则就是php的东西
进入data.php
页。
<?php
session_start();
function Plusifier($value = 0,$plus = 0)
{
// This is what does the adding...
$value += $plus;
return $value;
}
if(isset($_POST['submit'])) {
// Loop through POST arrays
foreach($_POST as $letter => $vals) {
// If not the submit button
if(is_array($vals)) {
// Loop through POST arrays
foreach($vals as $sub => $values)
// Assign sessions
$_SESSION[$letter][$sub] = Plusifier($values,(isset($_SESSION[$letter][$sub]))? $_SESSION[$letter][$sub]:0);
}
}
}
?><html>
<head>
</head>
<body>
<form method="post" action="data.php">
<table>
<tr>
<td>question1</td>
<?php for($i = 1; $i <= 10; $i++) { ?>
<td><input type="radio" name="D[1]" value="<?php echo $i; ?>"><?php echo $i; ?></td>
<?php } ?>
<td colspan="2">TOTAL: <?php echo (isset($_SESSION['D'][1]))? $_SESSION['D'][1]: 0; ?></td>
</tr>
<tr>
<td>question2</td>
<?php for($i = 1; $i <= 10; $i++) { ?>
<td><input type="radio" name="I[1]" value="<?php echo $i; ?>"><?php echo $i; ?></td>
<?php } ?>
<td colspan="2">TOTAL: <?php echo (isset($_SESSION['I'][1]))? $_SESSION['I'][1]: 0; ?></td>
</tr>
<tr>
<td>question3</td>
<?php for($i = 1; $i <= 10; $i++) { ?>
<td><input type="radio" name="H[1]" value="<?php echo $i; ?>"><?php echo $i; ?></td>
<?php } ?>
<td colspan="2">TOTAL: <?php echo (isset($_SESSION['H'][1]))? $_SESSION['H'][1]: 0; ?></td>
</tr>
<tr>
<td><label> question4 </label></td>
<?php for($i = 1; $i <= 10; $i++) { ?>
<td><input type="radio" name="A[1]" value="<?php echo $i; ?>"><?php echo $i; ?></td>
<?php } ?>
<td colspan="2">TOTAL: <?php echo (isset($_SESSION['A'][1]))? $_SESSION['A'][1]: 0; ?></td>
</tr>
<!-- 14 -->
<tr>
<td><label> question5 </label></td>
<?php for($i = 1; $i <= 10; $i++) { ?>
<td><input type="radio" name="D[2]" value="<?php echo $i; ?>"><?php echo $i; ?></td>
<?php } ?>
<td colspan="2">TOTAL: <?php echo (isset($_SESSION['D'][2]))? $_SESSION['D'][2]: 0; ?></td>
</tr>
<!-- 15 -->
<tr>
<tr>
<td colspan=2><div align="center"><input type="submit" name="submit" value="Score my test" ></div></td>
</tr>
</table>
</form>
</body>
</html>