• Resolved spacemakerman

    (@spacemakerman)


    Hello!
    I am using the Calculated Fields Form plugin to create a form on WordPress. I want a button to open a new window with WhatsApp Web and populate the form field values ​​in the message. I have written the following code in the OnClick event for the button:

    (function() {
    // Get field values
    const name = fieldname1; // Name
    const phone = fieldname2; // Phone
    const area = fieldname7; // Area

    // Create the message text (text for example)
    const message = encodeURIComponent(
    Hello, I am contacting you through your website. My name is ${name}, my phone number is ${phone}. I want to order project, area is ${area} m2.
    );

    // Create the WhatsApp Web link
    const whatsappUrl = https://web.whatsapp.com/send?phone=18002428478&text=${message};

    // Open WhatsApp Web in a new window
    window.open(whatsappUrl, '_blank');
    })();

    However, when I click the button, nothing happens. Could you please tell me what is wrong with this code? Why isn’t it working? Am I accessing the form fields incorrectly, or am I missing something? Thank you in advance for your help!

Viewing 1 replies (of 1 total)
  • Plugin Author CodePeople2

    (@codepeople2)

    Hello @spacemakerman

    Thank you so much for using our plugin. In Javascript, the texts must be enclosed between quotes.

    E.g:


    const message = encodeURIComponent(
    <code>Hello, I am contacting you through your website. My name is ${name}, my phone number is ${phone}. I want to order project, area is ${area} m2.</code>);

    You must use them in the URL variable too.

    Also, you can refer to the fields’ values directly by their names only in the equations’ content. If you want to access the fields’ values from another context, like the onclick event of a button, you must use the plugin operations like getField that returns an object representation of the fields, and calls its val method with two parameters:

    E.g.

     const name = getField('fieldname1').val(true, true);

    The first parameter in the val method tells the plugin you want to access the field’s raw value. The second one tells the plugin that you want it without extra double quote symbols.

    For additional questions, please provide the link to the page containing the form to check your code in action.

Viewing 1 replies (of 1 total)

The topic ‘Using Variables in WhatsApp Message Text’ is closed to new replies.