Calculated Fields Form Blog

Tips and cases of use for a successful WordPress website with calculated forms.

Blog / Is it possible to collect the user information without submitting the form? Not by default....but yes, you can.

Is it possible to collect the user information without submitting the form? Not by default....but yes, you can.

This post responds to the questions:

  • How to send the information collected by the form to the server without submitting it?
  • How to send this information by email?
  • How to manage this information in a safely?

I'll try to respond this questions with a real use case.

Suppose you have implemented a multi-pages form, where the first page collects the contact information of the user, and you want to receive an email with this information after the user populates these fields, and before he/she accesses to the other fields in the form.

I'll assume that the fields for the contact information are:

For the first name, the text field: fieldname1, for the last name, the text field: fieldname2, and for the email address the fieldname3

This project requires the "SERVER SIDE EQUATIONS" add-on (https://cff.dwbooster.com/add-ons/server-side-equations) for processing the information collected by the form, and send the emails.

  • First step in the process would be activate the "SERVER SIDE EQUATIONS" add-on.

Go to the settings page of the plugin, tick the "CFF - Server Side Equations" checkbox, and then, press the "Activate/Deactivate addons" button.

Add ons area

  • The previous action enables a new section in the settings page of the plugin for defining the server side equations. In this project, I'll call to the server side equation as: "sendmail", and it will require three parameters: $firstname, $lastname and $email:

    $GLOBALS['SERVER_SIDE_EQUATIONS']['sendmail'] = function($firstname, $lastname, $email){
        $firstname = sanitize_text_field($firstname);
        $lastname = sanitize_text_field($lastname);
        $email = sanitize_email($email);
    
        if(
            !empty($firstname) &&
            !empty($lastname) &&
            !empty($email)
        )
        {
            $message = "$firstname $lastname with email $email, is populating the form";
            return wp_mail("youremail@yourdomain.com", "Contact information", $message);
        }
        return false;
    };
    

Server side equation

The first step in the server side equation sanitizes the received parameters (NEVER TRUST in the data entered by the users), using the WordPress functions: sanitizetextfield and sanitize_email (more information about the sanitization functions in the following link: Validating Sanitizing and Escaping User Data)

The second step in the server side equation checks the parameters are not empty, avoiding send useless information.

Finally, the equation calls the wp_mail function for sending the notification email. wp_mail is a WordPress function where the first parameter is the destination email address, the second one, the email's subject, and the third parameter its body (more information about the wp_mail function in the following link: wp_mail)

As the wp_mail function returns a boolean, the server side equation returns the same result to know if the email was sent or not.

  • Once implemented the server side equation, is time to call it from the form.

As I said previously, the form includes three fields for the first name, last name and email, called: fieldname1, fieldname2 and fieldname3, respectively. I recommend you to configure them as required to ensure the user populates these fields before moving to the second page of the form.

Insert a calculated field in the form, for calling the server side equation, and enter the following piece of code as its equation:

    (function(){
        if('undefined' == typeof email_flag || !email_flag)
        {
            email_flag = SERVER_SIDE('sendmail', fieldname1, fieldname2, fieldname3);
        }
    })()

Client side equation

As the calculated field is being used as auxiliary, it is not essential in the form's interface, so, you should tick the checkbox: "Hide Field From Public Page" in its settings.

The client side equation, associated to the calculate field works as follows:

It checks if the global variable email_flag is defined and its value. If the variable exists, and its value is true, that means the server side equation as being evaluated previously, and has returned true, in whose case the email has been sent properly, and the server side equation should not be called again.

SERVER_SIDE is an operation included by the "SERVER SIDE EQUATIONS" add-on, whose first parameter is the name of server side equation, and the other parameters, the server side equation's parameters.

Note, that it is possible to reduce the calls to the server side equation, checking in the client side equation if the fields are not empty:

    (function(){
        if('undefined' == typeof email_flag || !email_flag)
        {
            if(AND(fieldname1, fieldname2, fieldname3))
            email_flag = SERVER_SIDE('sendmail', fieldname1, fieldname2, fieldname3);
        }
    })()

and that's all.

The form sends the information to the server with the equation (previous to the form's submission), and from the server side equation it sends the email, although it is possible to store the information in a database too, or call an external service.