Skip Navigation

[Resolved] Recording a default date from a form

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by nickH-5 5 months, 2 weeks ago.

Assigned support staff: Minesh.

Author
Posts
#2405253

I have a form which has a start and end date for an event.

I'd really like to be able to default the end date to the same as the start date unless the user changes the end date. So, once the user enters the start date it is duplicated in the end date and (if the user doesn't change the end date) is recorded in the wpcf-end-date field.

Is there a way to achieve this please?

Thanks

#2405927

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you want to copy the date via server side that is when user select the start date and submit the form, it should copy the start date to end date field? or you want that as soon as user select the date from start date field then using the custom Javascipt you want to copy the start date to end date field?

#2406105

Actually either would work provided that the user - while in the form - can amend the pre-filled end date before submitting the form.

Thanks

#2406235

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can use the Toolset form's hook "cred_save_data" to copy the field.

You can add the following code to "Custom Code" section offered by Toolset:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

For example:

add_action('cred_save_data','func_copy_start_end_field',10,2);
 function func_copy_start_end_field($post_id,$form_data) {
    $type = get_post_type($post_id);
    if ($form_data['id']==9999) {
         $start_date  = get_post_meta($post_id, 'wpcf-start-date', true);
        update_post_meta($post_id, 'wpcf-end-date', $start_date);
       
     }
}

Where:
- you can replace the 9999 with your original form ID
- replace the start and end date field slug where applicable.

#2406337

Thanks but isn't the problem with this option that, if the user enters an end date, it will be overwritten by the start date when the form entry is saved?

What I really need is the user can enter a start and end date but if they only enter a start date, the end date becomes the same as the start date.

Thanks

#2406367

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Nothing to worry still. You can still use the same hook. I've modified the code as given under:

add_action('cred_save_data','func_copy_start_end_field',10,2);
 function func_copy_start_end_field($post_id,$form_data) {
    $type = get_post_type($post_id);
    if ($form_data['id']==9999) {
       
         $end_date  = get_post_meta($post_id, 'wpcf-end-date', true);
        if (empty($end_date)) {
                     $start_date  = get_post_meta($post_id, 'wpcf-start-date', true);  
                    update_post_meta($post_id, 'wpcf-end-date', $start_date);
         }
        
     }
}

- Where:
- you can make the changes as applicable for field slugs and form ID.

As you can read the code, we are getting the end date value and store it to $end_date variable and we check that if the $end_date is empty then we get the start date field value and assign it to the end date field.

#2406455

Thank you that works exactly as I need. Really appreciate it.