在 php 中提交单选按钮变量

Submission of radio button variable in php

本文关键字:单选按钮 变量 提交 php      更新时间:2023-09-26

我正在编写一个程序,该程序将显示一个班级的学生列表,在每个学生姓名的前面有三个单选按钮,分别用于缺席,在场和请假。

学生列表是使用 while 循环生成的。现在我想通过这样的变量传递单选按钮的值。

echo "<tr>"
   . "<td>$id</td>"
   . "<td>$name</td>"
   . "<td><input type=radio name=$name value=P></td>"
   . "<td><input type=radio value=L name=$name></td>"
   . "<td><input type=radio name=$name value=A></td>"
   . "</tr>";

是否可以像这样发送单选按钮的值?

是的,但请注意,您的输入属性应包含单引号/双引号。

echo "<input type=radio name='$name' value='p' />";

或者你可以像这样转义双引号

echo "<input type=radio name='"$name'" value='"P'">";

或连接 PHP 变量

echo '<input type=radio name="'.$name.'" value="P">";

更新:检查每个学生的个人资料

  1. 在 while 循环中创建一个数组名称。数组名称是这样的,它在字符串"attendance"后面有方括号

    <input name="attendance[]" />
    
  2. 现在,对于每个学生的行。在阵列名称中分配学生 ID。

    echo "<tr>"
    . "<td>".$id."</td>"
    . "<td>".$name."</td>"
    . "<td><input type='radio' name='attendance[".$id."]' value='P'></td>"
    . "<td><input type='radio' name='attendance[".$id."]' value='L'></td>"
    . "<td><input type='radio' name='attendance[".$id."]' value='A'></td>"
    . "</tr>";
    
  3. 将$id放在数组名称中将用作指针,在后端使用它来更新每个学生的出勤状态。

    foreach ($_POST['attendance'] as $key => $value) {
    //$_POST['attendance'] variable is an array.
    //Where $key variable is your Student ID, use this to update their status
    //Where $value is a student's selected attendance status.
      echo 'Student ID:'. $key . ' is '. $value . '<br>';
      //Update a student's attendance status using $key as their id.
    }
    

你也可以模拟我做的这段代码:http://viper-7.com/Tb1lbo

是的,这是可能的。法典:

<?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
)

首先,您需要在名称周围包含单引号或双引号,如下所示:

<tr><td>$id</td><td>$name</td><td><input type='radio' name='$name' value='P'></td><td><input type='radio' value='L' name='$name'></td><td><input type='radio' name='$name' value='A'></td></tr>";

然后,您可以使用单选按钮组的名称从$_POST或超全局$_GET获取值,具体取决于您的请求类型。

$name = 'name_of_the_radios'
$studentPresence = $_POST[$name] //For get requests use $_GET
if($studentPresence == 'P')
{
    //Student is present
}
else if($studentPresence == 'A')
{
    //Student is absent
}
else if($studentPresence == 'L')
{
    //Student is on leave
}
else
{
    throw new Exception("Invalid student presence argument');
}