• Is anyone here able to help me? I’m using the Elementor contact form, and I’m facing an issue where the same user can submit the form multiple times using the same email address. I want to prevent this so that each email can only be used for a single submission. How can I prevent multiple submissions from the same email address?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @attaurrahman21, I’m here to help you.

    To make sure you don’t have to submit the same form more than once from the same email address, you can use a custom function in PHP to check if the email address has already been used. Let me show you how step-by-step:

    1. Create a custom function: This function will check if the email address exists in the database.
    2. Hook into Elementor form submission: Use Elementor’s action hook to validate the email before submission.

    Full Code
    Place this code in your theme’s functions.php file or in a custom plugin.

    // Add action hook for Elementor form submission
    add_action('elementor_pro/forms/new_record', function ($record, $handler) {
    // Make sure it's our form
    $form_name = $record->get_form_settings('form_name');
    if ('your_form_name' !== $form_name) {
    return;
    }

    // Get submitted fields
    $raw_fields = $record->get('fields');
    $fields = [];
    foreach ($raw_fields as $id => $field) {
    $fields[$id] = $field['value'];
    }

    // Check if email already exists
    $email = $fields['email']; // Replace 'email' with the actual field ID
    if (email_exists_in_submissions($email)) {
    // Prevent form submission and return error message
    $handler->add_error_message('This email address has already been used.');
    $handler->is_success = false;
    }
    }, 10, 2);

    // Function to check if email exists in submissions
    function email_exists_in_submissions($email) {
    global $wpdb;
    $table_name = $wpdb->prefix . 'elementor_submissions'; // Replace with your actual table name if different

    // Query the database for the email address
    $query = $wpdb->prepare(
    "SELECT COUNT(*) FROM $table_name WHERE field_email = %s",
    $email
    );
    $count = $wpdb->get_var($query);

    return $count > 0;
    }


    Explanation

    1. Hook into Elementor form submission:

    • The elementor_pro/forms/new_record action is used to hook into the form submission process.
    • The function checks if the form being submitted is the one we want to validate by comparing the form name.

    2. Get submitted fields:

    • Retrieve the form fields and their values.

    3. Check if email already exists:

    • The email_exists_in_submissions function queries the database to check if the email has been used.
    • If the email exists, an error message is added and the form submission is prevented.

    This setting will ensure that each email address can only be used for a single submission on your Elementor form.

    Please conduct some tests and let me know if everything is functioning as intended. If this code solves your problem, it would be advisable to mark this thread as ‘Resolved’.

    Best regards,
    Mateo

    If the answer above does not solve your question, I would recommend getting in touch with Elementors’s support about this via https://elementor.com/support/ if you have Elementor Pro or https://www.ads-software.com/support/plugin/elementor/ if you do not.

    Thread Starter attaurrahman21

    (@attaurrahman21)

    Yes, @threadi, I tried using the code with the same email field ID and correctly named the form, but it’s not working. Before posting here, I contacted Elementor’s live support, and they informed me that this feature is not possible in the current version of Elementor. They mentioned it might be added in a future update.

    Thread Starter attaurrahman21

    (@attaurrahman21)

    @Mateo Do you have another solution? This code isn’t working for me.

    You can use this code. please update your form name.

    class MuktoEmailRestrictionHandler {
    private $wpdb;
    private $table_prefix;
    private $target_form_names;
    private $submissions_table;
    private $values_table;


    public function __construct($target_form_names) {
    global $wpdb;
    $this->wpdb = $wpdb;
    $this->table_prefix = $wpdb->prefix;
    $this->target_form_names = $target_form_names;
    $this->submissions_table = $this->table_prefix . 'e_submissions';
    $this->values_table = $this->table_prefix . 'e_submissions_values';

    // Register the validate_email method as a callback for the elementor_pro/forms/validation/email action hook.
    add_action('elementor_pro/forms/validation/email', array($this, 'validate_email'), 10, 3);
    }

    /**
    * Validate the email address being submitted.
    *
    * @param array $field The email field data.
    * @param \ElementorPro\Modules\Forms\Records\Record $record The form record object.
    * @param \ElementorPro\Modules\Forms\Submissions\Ajax_Handler $ajax_handler The AJAX handler object.
    */
    public function validate_email($field, $record, $ajax_handler) {
    $form_name = $record->get_form_settings('form_name');

    // Check if the current form is one of the target forms.
    if (!in_array($form_name, $this->target_form_names)) {
    return;
    }

    $invalid_emails = $this->get_submitted_emails($record->get_form_settings('id'), $form_name);

    // Check if the email address being submitted is already present in the list of invalid emails.
    if (in_array($field['value'], $invalid_emails)) {
    $ajax_handler->add_error($field['id'], "You have already submitted with this email!");
    }
    }

    /**
    * Retrieve the email addresses that have already been submitted for a specific form.
    *
    * @param int $form_id The form ID.
    * @param string $form_name The name of the form.
    * @return array An array of email addresses that have already been submitted for the given form.
    */
    private function get_submitted_emails($form_id, $form_name) {
    $form_submissions = $this->wpdb->get_results(
    $this->wpdb->prepare(
    "SELECT id FROM {$this->submissions_table} WHERE form_name = %s",
    $form_name
    )
    );
    $form_submission_ids = array_column($form_submissions, 'id');

    if (empty($form_submission_ids)) {
    return array();
    }

    $placeholders = rtrim(str_repeat('%d,', count($form_submission_ids)), ',');
    $results = $this->wpdb->get_results(
    $this->wpdb->prepare(
    "SELECT s.value AS email
    FROM {$this->values_table} AS s
    INNER JOIN (
    SELECT submission_id, MAX(id) AS max_id
    FROM {$this->values_table}
    WHERE
    key = 'email'
    GROUP BY submission_id
    ) AS e ON s.submission_id = e.submission_id AND s.id = e.max_id
    WHERE s.submission_id IN ($placeholders)",
    $form_submission_ids
    )
    );

    return array_column($results, 'email');
    }
    }

    // Create an instance of the MuktoEmailRestrictionHandler class and pass the array of target form names as an argument.
    $email_restriction_handler = new MuktoEmailRestrictionHandler(array('update this form name'));
Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.