简体   繁体   中英

How to control radio buttons? data echoed from database, PHP, MYSQL

I am having a problem on sorting out the radio button while creating attendance interface for the teachers. I'm trying to get the right radio button checked when I pull data from a mysql database to display in an edit form.

There is a table of four columns where the columns are separated as names, present, absent and late. The list of names are echoed out in the webpage by fetching MYSQL database that means if a new student is added in the list then it will be echoed in the webpage along with the absent, present and late option automatically. Please have a look at the code below which I have used to echo out the list of names and other options individually.

          while($row = mysqli_fetch_array($result))
           {

   echo "<tr>
   <td><img......../></td> 
   <td>" .$row['name']. $row['surname']."</td>  
   <td><input type='radio' name='attend' value='present' /> Present &nbsp;
   <input type='radio' name='attend' value='absent' /> Absent &nbsp;
   <input type='radio' name='attend' value='late' /> Late &nbsp; 
   <td>
    </tr>";
    }

   echo "<input class='attendanceSubmitBtn' type='submit' name='btnSubmit' value='Submit'>"; 
   echo "</table>"; 
   echo "</form> <br/>"; 
   ?>

To operate function

 foreach ($_POST['attend'] as $attendance => $value) {

 if($value == 'present') {

  $query = "INSERT INTO Test (date, status) VALUES (NOW(), '1')";  
   $close = mysqli_query($con, $query); 
  echo "present done properly"; 

You are displaying it wrongly. You need to provide different name for each attendance.

Do it like this:

 while($row = mysqli_fetch_array($result))
           {

   echo "<tr>
   <td><img......../></td> 
   <td>" .$row['name']. $row['surname']."</td>  
   <td><input type='radio' name='attend".$row['name']."' value='present' /> Present &nbsp;
   <input type='radio' name='attend".$row['name']."' value='absent' /> Absent &nbsp;
   <input type='radio' name='attend".$row['name']."' value='late' /> Late &nbsp; 
   <td>
    </tr>";
    }

   echo "<input class='attendanceSubmitBtn' type='submit' name='btnSubmit' value='Submit'>"; 
   echo "</table>"; 
   echo "</form> <br/>"; 
   ?>

As you can see I replaced name='attend' with name='attend".$row['name']."' but to make it even better you should replace the $row['name'] there with a unique id, like primary key.

When you need to loop through all attend values you will need to loop through all names.

    foreach ($names as $name)
    {
    $value=$_POST['attend'.$name]; //this value could be absent , present or late
echo $name.' '.$value.'<br>'; //You have the name as well as value. Now you can insert or Update them in the database within this loop itself.
    }

But again I strongly suggest you to use a primary key instead of name there. Else you will have problems when names are same for two persons.

I think we're missing quite a bit of information to properly help you, but I hope you can use this bit of code I've put together for you. Assuming that the 'status' in the database for present is 1 (as seen in your example), for absent it's 2, and for late it's 3. Also I assume that your query on the top of your result contains this status somehow. Also, I've created a form for each table row, because I haven't seen any teacher ID to assign an array to the checkboxes' name tags.

Your PHP code to process the form:

<?php
if ($isset($_POST['attend']) {
  switch ($_POST['attend']) {
    case 'present':
      $status = 1;
      break;
    case 'absent':
      $status = 2;
      break;
    case 'late':
      $status = 3;
      break;
    default:
      $status = 0;
      break;
  }
  if ($status != 0) {
    $query = "INSERT INTO Test (date, status) VALUES (NOW(), " . $status . ")";
    $close = mysqli_query($con, $query);
    echo $_POST['attend'] . " done properly";
  }
}
?>

Your form (I guess you have an opening <table> tag and a table header somewhere before this):

<?php
$result = mysqli_query(" WHATEVER YOUR QUERY IS ");

$statuses = array(1 => 'Present', 2 => 'Absent', 3 => 'Late');

while ($row = mysqli_fetch_array($result)) { ?>
  <tr>
    <td><img......../></td> 
    <td><?php echo $row['name'] . " " . $row['surname']; ?></td>  
    <td>
    <form action="" method="post">
      <?php foreach ($statuses as $status) { ?>
        <input type="radio" name="attend" value="<?php echo strtolower($status); ?>"<?php echo ($row['status'] == $status) ? ' checked' : ''; ?> /> <?php echo $status; ?> &nbsp;
      <?php } ?>
      <input class='attendanceSubmitBtn' type='submit' name='btnSubmit' value='Submit'> 
    </form>
    </td>
  </tr>
<?php } ?>
</table>
<br/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM