Get table data value when selecting radio

Hello. I have this page where I am displaying rows with radio buttons in front of each row.


By selecting the radio for the second row, I am trying to get the id of that row which is in the variable $row['class_id'] (8). The same selecting the first row I am trying to get the id of 1.
Doing this having displayed another table “members” I can select one row in the same way and get member_id. Getting this variables, I can then pass them to a PHP function. that will basically say: the member with this id, is doing the class with this id.
Any suggestions? Is it the right approach to do this?

This is what I have for now, but it’s not getting the id of the row. How I have my code now it should display it.

<script type='text/javascript' src='//code.jquery.com/jquery-2.1.0.js'></script>
      <?php 
       
      include("header.html");
      include("menu.html");   
      ?>
      <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
      <h2 class="sub-header">Gym Members</h2>
            <div class="row">
              <div class = "col-sm-12">
              <div class="table table-bordered">
                  <table class="table table-striped">
                    <thead>
                      <tr>
                        <th></th>
                        <th>ID</th>
                        <th>Type</th>
                        <th>Name</th>
                        <th>Duration</th>
                        <th>Location</th>
                        <th>Date</th>
                        <th>Repeat</th>
                        <th>Description</th>
                      </tr>
                    </thead>
                    <tbody>

                      <?php 
                      require_once ('database_initialisation.php');
                      $table2 = "clases"; 
                      $array = $admin_query->viewTableData($table2);
                      foreach ($array as $row)
                      {
                      echo 
                            "<tr id = `testid`>
                             <td> <input type = 'radio' name = 'classRadio' id = 'classRadio' /> </td>
                             <td>" . $row['class_id'] . "</td>
                             <td>" . $row['class_type'] .  "</td>
                             <td>" . $row['class_name'] .  "</td>
                             <td>" . $row['class_duration'] .  "</td>
                             <td>" . $row['class_location'] .  "</td>
                             <td>" . $row['class_date'] .  "</td>
                             <td>" . $row['class_repeat'] .  "</td>
                             <td>" . $row['class_description'] .  "</td>
                        </tr>";
                      } ?>

                    </tbody>
                  </table>
                </div>
              </div>
            </div>    
            <div id='checkvalue'>
            </div>
      <script>

            $('tbody').on('change', ':radio', function()
                  {
                   var $row = $(this).parent().parent();
                    var valueOfRadio = $('input[name=classRadio]:checked').val();
                    var idOfRow = $row.attr('id');
                  $('#checkvalue').html($('input[name=classRadio]:checked').val());
                 // alert($('input[name=classRadio]:checked').val());

                 });
      </script>
      </div>

Personally I think you would be bettor off having an additional field(column) in the students database, maybe schedule where it would contain the list of courses or even have a separate database table with the student id, course number, course name, etc. That way you wouldn’t have to try to ‘pick’ out the courses and then for example you could simply do a search on a person to see what courses they are currently taking.

@Pepster I have another table member_registration that contains member_id and course_id. This way I can have multiple records of the same student with different classes. What I was trying to ask, is interface wise, how should I implement this. The most simple way, I could have two fields for member_id and corse_id, and pass the input values to the third table.( for example I know the member with id x wants to join class y, so I just input x and y).
Something more interactive, will be having the original tables of members and classes with their relevant information displayed, and then just clicking in the member table on a member, and classes table on a class. This would get the clicked member id and the clicked class id, and on submit it would store this values on the class_registration table.
Don’t know if you understand what I mean. The issue here is to implement this row click and selection for both tables. On click of a row in the members table get somehow the member’s id, and then clicking in the classes table on another row get that rows class id. On submit store thees value in the table class_registration.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.