是的,這是可能的。代碼:
<?php
$students = [
[
'id' => '1',
'name' => 'Amy'
],
[
'id' => '2',
'name' => 'Bob'
],
[
'id' => '3',
'name' => 'Charlie'
]
];
echo
'<form action="' . $_SERVER['PHP_SELF'] . '" method="post">' .
'<table>' .
' <thead>' .
' <tr>' .
' <th>Student Id</th>' .
' <th>Student Name</th>' .
' <th>Attendance (P/L/A)</th>' .
' </tr>' .
' </thead>' .
' <tbody>';
foreach ($students as $student) {
echo
' <tr>' .
' <td>' . $student['id'] . '</td>' .
' <td>' . $student['name'] . '</td>' .
' <td>' .
' <input type="radio" name="attendance_' . $student['id'] . '" value="P" />' .
' <input type="radio" name="attendance_' . $student['id'] . '" value="L" />' .
' <input type="radio" name="attendance_' . $student['id'] . '" value="A" />' .
' </td>' .
' </tr>';
}
echo
' <tr>' .
' <td colspan="3">' .
' <input type="submit" name="submit" value="Attendance" />' .
' </td>' .
' </tr>' .
' </tbody>' .
'</table>' .
'</form>';
if (isset($_POST['submit'])) {
echo '<pre>' . print_r($_POST, true) . '</pre>';
}
而且在形式提交:
Array
(
[attendance_1] => P
[attendance_2] => L
[attendance_3] => L
[submit] => Attendance
)
是的。但是對於所有三個單選按鈕,名稱應該相同,並且不應爲其他記錄或其他單選按鈕組重複。保持名稱,如'name ='your_name''。你也可以使用變量傳遞按鈕的'value'。 –