<?php
include('connect.php');
$query=mysqli_query($conn,"select * from users") or die("unable to connect");
?>
<html>
<head>
</head>
<body>
<h2 align="center"> student list</h2>
<form action="radio.php" method="post">
<table align="center" border="1" cellspacing="0" cellpadding="0" width="700">
<thread>
<td align='center'>id</td>
<td align='center'>enrolloment_no</td>
<td align='center'>first_name</td>
<td align='center'>last_name</td>
<td align='center'>attendance </td>
</thread>
<?php
$i=1;
while($row=mysqli_fetch_array($query,MYSQLI_BOTH))
{
echo "<tr>
<td align='center'>".@$i."</td>
<td align='center'>".@$row['enrolloment_no']."</td>
<td align='center'>".@$row['first_name']."</td>
<td align='center'>".@$row['last_name']."</td>
<td align='center'>
<input type=\"radio\" name=\"a\" value=\"present\">present
<input type=\"radio\" name=\"a\" value=\"absent\">absent
</tr>
";
$i++;
}
?>
</table>
<input type="submit" value="submit">
</form>
</body>
</html>
The above mentioned code contains a attendance page form which contains radio buttons but the main problem in the above code is i am unable to select radio button from each row as i need to select radio button from each row that is either student would be present or absent so please if anyone has any suggestions how to select one radio button either present or absent from each row and then submit to database.
You can create a number increasing by each row, or better, set this integer equal to the row's corresponding identifier in your database.
Add a hidden input with name="rowIDs[]" & value="{theIdentifier}".
When you set the name of your row's radio button to "a_{theIdentifier}" you can run a foreach in your PHP script.
might this be what you are looking for?
global example in your code:
$i=1;
while($row=mysqli_fetch_array($query,MYSQLI_BOTH)){
echo "<tr>
<td align='center'>".@$i."</td>
<td align='center'>".@$row['enrolloment_no']."</td>
<td align='center'>".@$row['first_name']."</td>
<td align='center'>".@$row['last_name']."</td>
<td align='center'>
<input type=\"hidden\" value=\"".$row['enrolloment_no']."\" name=\"rowIDs[]\" />
<input type=\"radio\" name=\"a_".$row['enrolloment_no']."\" value=\"present\">present
<input type=\"radio\" name=\"a_".$row['enrolloment_no']."\" value=\"absent\">absent
</td>
</tr>
";
$i++;
}
Now in the submission:
$rowIDs = $_POST['rowIDs'];
foreach($rowIDs as $rowID) {
$radioButtonValue = $_POST['a_'.$rowID];
mysqli_query("update `yourtable` SET `attendance` = '".$radioButtonValue."' WHERE `enrolloment_no` = '".$rowID."'");
}
(to do this, replace $i by $row['enrolloment_no'])
ps i made some mistakes i just edited.
When you add these inputs on the top of the form, or bottom.. but IN the form tags Present
Absent
Use master attendance
Then in the submission php part:
if(isset($_POST['useMasterAttendance'])) {
$masterAttendanceValue = $_POST["masterAttendance"];
$rowIDs = $_POST['rowIDs'];
foreach($rowIDs as $rowID) {
mysqli_query("update `yourtable` SET `attendance` = '".$masterAttendanceValue."' WHERE `enrolloment_no` = '".$rowID."'");
}
}else {
//DO THE CODE FOR EACH ROW LIKE ABOVE
}
Sorry if i made any typos
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments