Skip Navigation

[Resolved] Multiply two custom fields | CRED form

This support ticket is created 4 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.

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 Lara 4 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#1601685

Tell us what you are trying to do?
I need to multiply the value of two custom fields with JavaScript and display the result in a third custom field in a CRED form

My CRED form:

[credform]

[cred_field field='menge' force_type='field' class='bw-inputfield' output='bootstrap']
[cred_field field='produktpreis' force_type='field' class='bw-inputfield' output='bootstrap']

Gesamtpreis:
[cred_field field='gesamtpreis' force_type='field' class='bw-inputfield' output='bootstrap']

[cred_field field='form_submit' output='bootstrap' value='Angebot einstellen' class='button-primary']
[/credform]

JavaScript:

jQuery( function( $ ) {
  
$('input[name=menge]').change(compute);
$('input[name=produktpreis]').change(compute);
  
function compute() {

   if ( $('input[name=produktpreis]').val() != undefined ) {
     
        var a = $('input[name=menge]').val();
        var b = $('input[name=produktpreis]').val();
        var gesamtpreis = a * b;

        $('input[name=gesamtpreis]').val(gesamtpreis);
     
   }
}
  
} );

Is there any documentation that you are following?
https://toolset.com/forums/topic/cred-calculation-fields-with-javascript/

Is there a similar example that we can see?
In the documentation.

What is the link to your site?
hidden link

#1602425

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

The thing is that if you created the custom fields using Types plugin the Types fields have prefix wpcf-

So, the field name menge should actually be accessed using name wpcf-menge.

Can you please try to add prefix wpcf- to your all custom fields and check if that help you to resolve your issue.

For example:

jQuery( function( $ ) {
   
$('input[name=wpcf-menge]').change(compute);
$('input[name=wpcf-produktpreis]').change(compute);
   
function compute() {
 
   if ( $('input[name=wpcf-produktpreis]').val() != undefined ) {
      
        var a = $('input[name=wpcf-menge]').val();
        var b = $('input[name=wpcf-produktpreis]').val();
        var gesamtpreis = a * b;
 
        $('input[name=wpcf-gesamtpreis]').val(gesamtpreis);
      
   }
}
   
} );
#1602953
2020-04-27_bywood.PNG

Hi Minesh,

I changed it as you suggested. Now it (input[name=wpcf-gesamtpreis]) throws back a zero.
Please view the picture 🙂

#1603525

Minesh
Supporter

Languages: English (English )

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

Can you please share the problem URL where you added the form as well as access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) 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.

#1608939

Hi Minesh,

many apologies for the delay. I can't share the credentials with you, before my boss approved it. Unfortunatelly I am still waiting for his approval. I will notify you as soon as I have the approval. I am truely sorry for the delay.

Kind regards
Lara

#1610733

Minesh
Supporter

Languages: English (English )

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

No issues, please let me know when you have it 🙂

#1614119

Hi Minesh,

many thanks for your help. While I was unwell my work collegue figured out, that we accidently inserted the form two times on the same page. So we had two "Gesamtpreis custom fields" on the page and the JavaScript didn't knew where it should output the results. This caused the issue. After my work collegue removed one of the forms, the remaining form started to work perfectly.

For everyone who wants to calculate something with CRED on the fly, I can really recommend this JavaScript snipplet.

Thanks 🙂