skip to Main Content

The actual form :

    <div class="form-input">
        <label for=""><?php esc_html_e( 'NAME', 'daily-attendance' ); ?></label>
        <input type="text" required readonly value="<?php echo esc_attr( $current_user->display_name ); ?>">
    </div>
    
    
    <div class="form-input">
        <label for=""><?php esc_html_e( 'DATE', 'daily-attendance' ); ?></label>
        <input type="text" required readonly
               value="<?php echo esc_attr( date( 'jS M, Y', current_time( 'timestamp' ) ) ); ?>">
    </div>
    
    <div class="form-input">
        <label for=""><?php esc_html_e( 'TIME', 'daily-attendance' ); ?></label>
        <input type="text" required readonly
               value="<?php echo esc_attr( date( 'h:s A', current_time( 'timestamp' ) ) ); ?>">
    </div>
    
    
    <div class="form-submit">
        <?php wp_nonce_field( 'pbda_nonce', 'pbda_nonce_value' ); ?>
        <button class="pbda-button" type="submit"><?php esc_html_e( 'SUBMIT', 'daily-attendance' ); ?></button>
    </div>

Then here is the a part of the functions.php to modify… :

if ( ! function_exists( 'pbda_insert_attendance' ) ) {
/**
* Insert new attendance
*
* @param bool $user_id
*
* @return false|int|WP_Error
*/
function pbda_insert_attendance( $user_id = false ) {

        $user_id = ! $user_id || empty( $user_id ) || $user_id === 0 ? get_current_user_id() : $user_id;
    
        if ( ! $user_id || empty( $user_id ) || $user_id === 0 ) {
            return new WP_Error( 'invalid_user', esc_html__( 'Invalid user information', 'daily-attendance' ) );
        }
    
        $user      = get_user_by( 'ID', $user_id );
        $report_id = pbda_current_report_id();
    
        if ( ! $report_id || empty( $report_id ) || $report_id === 0 ) {
            return new WP_Error( 'invalid_report', esc_html__( 'Something went wrong with Report ID', 'daily-attendance' ) );
        }
    
        if ( pbda_is_duplicate_attendance( $user_id, $report_id ) ) {
            return new WP_Error( 'duplicate', sprintf( esc_html__( 'Un prêt a déjà été consenti pour la route %s, aujourd'hui!', 'daily-attendance' ), $user->display_name ) );
        }
    
        $args = array(
            'user_id'      => $user_id,
            'report_id'    => $report_id,
            'date'         => date( 'Y-m-d', current_time( 'timestamp' ) ),
            'current_time' => current_time( 'timestamp' ),
        );
    
        $response = add_post_meta( $report_id, 'pbda_attendance', $args );
    
        if ( $response && is_int( $response ) ) {
            return sprintf( esc_html__( 'Hello %s, was added with success', 'daily-attendance' ), $user->display_name );
        }
    
        return false;
    }

}

What do i need to modify to put ny new field and the data of that field in the database ??

expecting to enter a 7 digit numeric value in the database and be able to retrieve this information. The numeric value is mandatory for my project. An employee enter his id number when he come to work

Where i am blocked :

I tried in the last 4 hours to add a $var input in the form and have it written in the database. The $var is not set in the database at all. I guess i am making errors between the html and php to send that variable into the database.

i tried to add another $arg in the array but when i am looking in the database, the variable is not passing to it…

Stuck!

2

Answers


  1. Halo dude, to integrate a numeric employee ID into your attendance form and store it in the database, start by updating your HTML form to include a new input field for the employee ID, ensuring it has a name attribute and appropriate validation pattern. Next, modify your pbda_insert_attendance function to accept this new employee_id parameter, validate and sanitize it to ensure it’s a 7-digit numeric value, and then include it in the data saved to the database. Finally, handle the form submission by capturing the employee ID from the POST data, verifying the nonce for security, and then processing the data by calling the updated function. This process ensures that the employee ID is correctly captured, validated, and stored securely.

    I hope this answer, solved your problem, thanks!

    Login or Signup to reply.
  2. Any form should be encapsulated within a form element like this:

    <form action="/submit" method="post">
      ... all your stuff goes here
    </form>
    

    If you don’t use a form element, then you have no way to submit that data (via POST) on the form page.

    You should also read this basic tutorial on form handling:
    https://www.w3schools.com/html/html_forms.asp

    The next thing you should do is employ basic checking methods to see if the data is posting, and have mechanisms in place if empty fields are being submitted. I would never rely on pure HTML on the page to handle this – but just use it together with php checks on the backend side of things.

    Here is an example with basic checklists for you to study up on:

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    
    ... Collect form data
    ... Make sure there are no empty fields
    ... Perform functions to sanitize and validate the data
    ... And finally insert that data to your corresponding DB table
    
    } 
    

    I "get" that you’re using read-me-only on a lot of these fields, but it may open a can of worms should those client-side checks fail.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search