Support » Plugin: Contact Form DB » Preventing Duplicate Submissions from CF7

  • Dear Michale, the script below is working perfectly with me, I want to validate another filed along with the email field, can you guide me to add another field filter with your script.

    /**
     * @param $formName string
     * @param $fieldName string
     * @param $fieldValue string
     * @return bool
     */
    function is_already_submitted($formName, $fieldName, $fieldValue) {
        require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
        $exp = new CFDBFormIterator();
        $atts = array();
        $atts['show'] = $fieldName;
        $atts['filter'] = "$fieldName=$fieldValue";
        $atts['unbuffered'] = 'true';
        $exp->export($formName, $atts);
        $found = false;
        while ($row = $exp->nextRow()) {
            $found = true;
        }
        return $found;
    }
     
    /**
     * @param $result WPCF7_Validation
     * @param $tag array
     * @return WPCF7_Validation
     */
    function my_validate_email($result, $tag) {
        $formName = 'email_form'; // Change to name of the form containing this field
        $fieldName = 'email_123'; // Change to your form's unique field name
        $errorMessage = 'Email has already been submitted'; // Change to your error message
        $name = $tag['name'];
        if ($name == $fieldName) {
            if (is_already_submitted($formName, $fieldName, $_POST[$name])) {
                $result->invalidate($tag, $errorMessage);
            }
        }
        return $result;
    }
     
    // use the next line if your field is a **required email** field on your form
    add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
    // use the next line if your field is an **email** field not required on your form
    add_filter('wpcf7_validate_email', 'my_validate_email', 10, 2);
     
    // use the next line if your field is a **required text** field
    add_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);
    // use the next line if your field is a **text** field field not required on your form 
    add_filter('wpcf7_validate_text', 'my_validate_email', 10, 2);
Viewing 1 replies (of 1 total)
  • Hi Rosh and Michaelle,
    thanks for the plugin and this script.

    I have got another idea on the same toppic. I need to keep only last version of submission and automatically delete previos for specific user. I can call this cleanup scipt by cron.

    It can work this way:
    “If is duplicated record name, surname and e-mail, delete the record with older submission date/time.”

    But I don’t know how figure out the PHP/SQL query 🙂

Viewing 1 replies (of 1 total)
  • The topic ‘Preventing Duplicate Submissions from CF7’ is closed to new replies.