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.
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?
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.
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.