最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

ajax - Updating a checkbox value to database for specific row in table

matteradmin8PV0评论

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.

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.

Share Improve this question edited May 21, 2017 at 0:26 David Lee 3,9413 gold badges15 silver badges20 bronze badges asked May 20, 2017 at 23:05 lukeliasilukeliasi 1112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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).

Post a comment

comment list (0)

  1. No comments so far