簡體   English   中英

while循環內有多個單選按鈕

[英]Multiple radio buttons inside while loop

嗨,大家好,我試圖在此程序中構建一個簡單的班級出勤系統,首先是我從名為class1的表中獲取名稱值,然后試圖在表名class1attendance中插入該名稱和出勤率。 但是,當我嘗試提交它時,無論我選擇第一個單選按鈕如何,我都為所有學生獲得相同的價值,例如,當我為理查選擇當前單選按鈕時,我有很多學生的第一個學生是理查德。學生們。 這是我的代碼

<?php 

include('header.php');
include('mysqli_connect.php');
echo "<br> <br>";

$q = "SELECT CONCAT(first_name, ' ', last_name) AS Name FROM class1";

$r = mysqli_query($dbc, $q);

echo '<div class = "container">';

echo '

<table>

<tr class = " w3-padding w3-green w3-xlarge "> <td> Student Name </td>

<td> Absent/Present </td>

 </tr>';

 $attendance ; 

 if($_SERVER['REQUEST_METHOD'] == 'POST')
{
     $attendance ; 
    if($_POST['attendance'] == 'present')
    {
        $attendance = 'present';
    }
    else
    {
        $attendance = 'absent';
    }

}

while($row = mysqli_fetch_array($r))
{
    $name =  $row['Name'];
    $q1 = "INSERT INTO class1attendance(s_id, first_name, attendance) VALUES (0, '$name', '$attendance')";

    $r1 = mysqli_query($dbc, $q1);
    echo '<tr> 
    <td>' . $name . '</td> <td>
    <form method = "post" action = "">
    Present <input type = "radio" name = "attendance" value = "present" class = "w3-radio">
    Absent <input type = "radio" name = "attendance" value = "absent" class = "w3-radio">
    </td>
    <td>
    <input type = "submit" name = "submit" value = "Submit Attendence" class = "w3-btn w3-green w3-round">
    </td>
    </tr>
    </form>';;


}


echo '</div>
</table>';


?>

所有單選按鈕組都需要唯一的names

每個學生都有一個小組:“缺席”和“出席”。

所以你需要做類似的事情

name=" $first_name + $last_name +'attendance'"  

Idk PHP,但這就是這個主意。 否則,您僅產生一個無線電組,因此得到一個值。

____編輯____

最終,您希望這樣的HTML:

<table>
    <!-- radio group 1 -->
    <tr>
      <td>
        John Doe
      </td>
      <td>
        <label for="JohnDoe0">
          Absent
          <input id="JohnDoe0" type="radio" name="JohnDoe_attendance" value="absent"/>
        </label>
      </td>
      <td>
        <label for="JohnDoe1">
          Present
          <input id="JohnDoe1" type="radio" name="JohnDoe_attendance" value="present"/>
        </label>
      </td>
    </tr>
    <!--/ end radoi group 1 -->

    <!-- radio group 2 -->
    <tr>
      <td>
        Jane Doe
      </td>
      <td>
        <label for="JaneDoe0">
          Absent
          <input id="JaneDoe0" type="radio" name="JaneDoe_attendance" value="absent"/>
        </label>
      </td>
      <td>
        <label for="JaneDoe1">
          Present
          <input id="JaneDoe1" type="radio" name="JaneDoe_attendance" value="present"/>
        </label>
      </td>
    </tr>
    <!--/ end radoi group 2 -->
  </table>

因此,為了正確接收您的值,每個無線電組的每個名稱都必須唯一。

因此,如果您有20個學生,那么您就有20個唯一的name屬性。 每個學生有2個單選按鈕。 這兩個單選按鈕都獲得該名稱。

注意:我不會在表格中放置表格。 我會使用CSS格式化表格。 但是,我將在表格中顯示該表單的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM