1

I have written a custom plugin for people to sign up to a course. The data is submitted in a front end form and stored in a custom table in the database. There is a "course_completed" column that is a boolean that by default is set to 0 (not checked/false).

Then I have a table that gets the data from the database of the people who have registered to the course. Once the course is over the admin should be able to check who has completed the course and click save. I am struggling to find a way that once the checkbox is ticked for a person, their "completed_course" value is changed to 1 in the database.

Code to display database data in table on the backend:

<tbody class="alternate">
            <?php
                global $wpdb;
                $table_name = $wpdb->prefix . 'crm';
              $result = $wpdb->get_results ("SELECT * FROM $table_name");
            ?>
            <?php foreach ($result as $field):  ?>
            <tr>
                <td><?php esc_attr_e( $field->id, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->time, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->first_name, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->last_name, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->email, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->phone, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->postcode, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->city, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->address, 'wp_admin_style')?></td>
            <td><?php esc_attr_e( $field->course, 'wp_admin_style')?></td>
            <td><input type="checkbox" name="course_completed" value="1" <?php checked( $field->course_completed, 1 ); ?> /></td>
            </tr>
            <?php endforeach; ?>
        </tbody>

Not sure if I am supposed to be using AJAX, a form inside the table or something else.

mysql backend table

1 Answer 1

1

Rough answer to get you started.

Inside the loop, use the row's record number (a field called 'IDnumber', unique, auto) to help name the of a .

The hidden <input> should have an id of something like

<input hidden name='idnumber' value='<?php echo $row[idnumber];?>'>

which will cause the vaue of that input field to be the record number of that record. (The <input> statements should be outside of PHP blocks.)

Use the $_POST values to determine which record to update by using the value of the hidden input item ('idnumber') - which will be the record number you want to update with the other values in the form.

The loop should be surrounded by a <form> block (outside the loop).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.