Skip Navigation

[Resolved] Copy field value to another field if generic checkbox is checked

This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 0.62 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9: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: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 3 voices.

Last updated by jamesR-13 2 years, 5 months ago.

Assisted by: Jamal.

Author
Posts
#2213173

I have a form that takes a billing address with slug 'billing-address'.

I have two more fields that take service addresses - 'residential-service-address' and 'commercial-service-address'.

I want to have a generic checkbox for each service address('res-serv-address-copy' and 'com-serv-address-copy'), that when checked will hide the associated service address field and copy the billing address field value to the appropriate service address field.

#2213601

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

You can achieve this with some basic JavaScript (or jQuery, which will be available on a page with a form).

You'll need to add an on change event listener to the generic checkboxes to capture when they are checked (see hidden link), and then you can copy the content from the billing-address field to the corresponding address field (you can use hidden link to both get and set the values).

It's a good idea to use the window.load event to trigger your code (which you can add in the custom JS section of your form), like so:

( function( $ ) {
    $( window ).on( "load", function(){
            		
        // add your jQuery code here

    });
})( jQuery );

If you are not comfortable writing jQuery (as above) or Javascript you can contact a developer familiar with Toolset on our contractors page (https://toolset.com/contractors/), although as it is not specific to Toolset any developer should be able to provide a solution.

#2213951

I don't have a budget for hiring another developer, and I am not strong with jquery/javascript.

The selling point for toolset is:

"Toolset Helps WordPress Professionals Build Advanced Sites Without Programming"

But I have been using these tools long enough to know that that's not 100% accurate, and some of the support techs on here seem perfectly willing to help with some code here and there, which seems to fit with the whole "...without programming" claim.

I was thinking I could do this in PHP using cred_save_data, but I don't know how to check the value of the generic checkbox field. Can you or someone else help me with this?

I have some code from another project I did where the value of one field is changed depending on the value of another field. Can you (or someone else) help me adapt that to use here?

add_action('cred_save_data_69', 'save_data_for_form_with_id_69',10,2);
function save_data_for_form_with_id_69($post_id, $form_data)
{
    $req_type = get_post_meta($post_id, '_wpcf_type_of_request', true);
    if($req_type == 'PTO'){    
        update_post_meta($post_id, 'wpcf-status', 'Processing');
        update_post_meta($post_id, 'wpcf-sub-status', 'Command/Department Head Approved');
    }
    else{
        update_post_meta($post_id, 'wpcf-status', 'Approved');
        update_post_meta($post_id, 'wpcf-sub-status', NULL);
    }
}

Maybe instead of a generic field, I should add the checkboxes as custom fields on the post?

#2214129

Based on what I posted above, I did go ahead and add the checkboxes as custom fields on the post type, and I modified the code like so:

add_action('cred_save_data_69', 'save_data_for_form_with_id_69',10,2);
function save_data_for_form_with_id_69($post_id, $form_data)
{
    $copy_res_address = get_post_meta($post_id, '_wpcf_res_serv_address_same_as_billing', true);
	$copy_com_address = get_post_meta($post_id, '_wpcf_com_serv_address_same_as_billing', true);
	$billing_address = get_post_meta($post_id, '_wpcf_billing_address', true);
    if($copy_res_address != ''){    
        update_post_meta($post_id, 'wpcf-residential-service-address', $billing_address);
    }
    if($copy_com_address != ''){
        update_post_meta($post_id, 'wpcf-commercial-service-address', $billing_address);
    }
}

Unfortunately, it doesn't copy the values from the Billing Address field to the service address field when the checkbox is checked. What am I missing?

Thanks for your help.

#2215097

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

You are right, we do provide some help with custom code. We do that for simple cases, but we don't do it for complex use cases or for repetitive requests. Custom code is still out of the scope of support.

Regarding your case, you should probably try this:

add_action('cred_save_data_69', 'save_data_for_form_with_id_69',10,2);
function save_data_for_form_with_id_69($post_id, $form_data)
{
    $copy_res_address = get_post_meta($post_id, '_wpcf_res_serv_address_same_as_billing', true);
    $copy_com_address = get_post_meta($post_id, '_wpcf_com_serv_address_same_as_billing', true);
    $billing_address = get_post_meta($post_id, '_wpcf_billing_address', true);
    if($copy_com_address == 1){
        update_post_meta($post_id, 'wpcf-commercial-service-address', $billing_address);
    } 
}

Keep in mind that this will work only for the form with ID 69, otherwise, you should adapt the first line to the form's ID. if it is 1234, then change the first line to:

add_action('cred_save_data_1234', 'save_data_for_form_with_id_69',10,2);

If this does not help, allow me temporary access to your website to check this further. Your next reply will be private to let you share credentials safely. ** Make a database backup before sharing credentials. **
Please let me know:
- A URL on the frontend where we can see the form.
- The URL of the form on the backend.
- The URL on the backend where we can see this custom code. Or FTP access if it is being added to the theme's functions.php file.

#2217791

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.