Skip Navigation

[Resolved] onClick function not working

This support ticket is created 2 years, 8 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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 Minesh 2 years, 8 months ago.

Assisted by: Minesh.

Author
Posts
#2321841

we want to copy a field value to another field using onClick function in submit button. we tried to use the following code but it is not working

submit btn code : [cred_field field='form_submit' output='bootstrap' value='Edit and Save' class='btn btn-primary btn-lg' onclick="copyval()"]

java script code
function copyval() {
var k=document.querySelector('input[name="wpcf-user-email"]').value;
document.querySelector('input[name="wpcf-user_email"]').value=k;
}

#2322505

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please explain me the goal of copying the field value to another field while you click on the submit button?
- Do you want to display the field value that you copy to another field on frontend? if no:
We can use the Toolset form's hook cred_before_save_data hook and assign/copy the field 1 value to field 2 value. does that makes sense?

#2322753

we have two email fields
One is the Main which is belong to WP users
The second is created by us (custom field)
we need both

when the user updates the secondary email in the user form, the main email should be updated directly with the same value.
So any email value entered in the second should be copied into the main.

the main will be hidden in the user form.

#2322755

Minesh
Supporter

Languages: English (English )

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

So why you will require javascript, have you tried to use the Toolset form's hook cred_before_save_data?

add_action('cred_before_save_data', 'func_update_user_email',10,1);
function func_update_user_email($form_data) {
    // if a specific form
    if ($form_data['id']==99999) {
        if (isset($_POST['secondary-email'])) {
           $_POST['primary-email'] = $_POST['secondary-email'];
        }
    }
}

Where:
- Replace "99999" with your original form ID
- you can adjust the code as required with original field slugs for primary and secondary email

#2323715

Hi Minesh,

thank you for your support,

I have tried the code you provide but still, the primary email did not change.

#2323777

Minesh
Supporter

Languages: English (English )

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

Can you please share problem URL and admin access details and for what user you want me to change the email address. Please share that user access details as well.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2323815

Minesh
Supporter

Languages: English (English )

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

The issue was you missed the "wpcf-" prefix before the custom field slug. So, the correct custom field slug will be:
wpcf-second-user-email

I've adjusted the code as given under to your code snippet.

add_action('cred_before_save_data', 'func_update_user_email',10,1);
function func_update_user_email($form_data) {
    // if a specific form
    if ($form_data['id']==5392) {
        if (isset($_POST['wpcf-second-user-email'])) {
           $_POST['user_email'] = $_POST['wpcf-second-user-email'];
        }
    }
}

Can you please check now it works as expected at your end as well:
- hidden link