【问题标题】:Submission of radio button variable in php在 php 中提交单选按钮变量
【发布时间】:2016-11-20 01:13:39
【问题描述】:

我正在编写一个程序,它将显示一个班级的学生列表,并且在每个学生姓名前面有三个单选按钮,分别代表缺席、出席和休假。

学生列表是用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>";

这样可以发送单选按钮的值吗?

【问题讨论】:

  • 是的。但是所有三个单选按钮的名称应该相同,并且对于其他记录或其他单选按钮组不应重复。保持名称如name='your_name'。您也可以使用变量传递按钮的value

标签: javascript php jquery css forms


【解决方案1】:

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

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

【讨论】:

  • thankx... 但是下一页应该是什么... $nameOpt=$_POST['$name'];我们可以这样写吗?
  • 是的,没错。分配 PHP 变量不需要引号,除非它是一个字符串。
  • 删除 $_POST['$name'] 上的单引号。 $name 是一个 php 变量而不是一个字符串。
  • 谢谢..它帮助了我......但我仍然有关于在下一页接收我想要的 P、A、L 的问题,以便我可以将出勤率保存在每个学生的个人资料表上。 .
【解决方案2】:

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

<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');
}

【讨论】:

  • while($row=mysql_fetch_array($result)) { $id=$row['id']; $name=$row['name']; echo $id $name "; } 这是我的代码
【解决方案3】:

是的,这是可能的。代码:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2013-03-28
    • 2012-04-05
    • 1970-01-01
    • 1970-01-01
    • 2014-07-29
    • 2014-12-07
    相关资源
    最近更新 更多