• Resolved X

    (@laksoner)


    The question has been already asked multiple times without a proper answer so it seems that it is a highly requested feature and i try it again…

    Im looking for a way to check if there is already a submission with the same email adress for example and show an error / prevent saving again to the database if so.

    I came across a possible solution here but it does’nt seem to work!?

    add_action( 'wpcf7_before_send_mail', 'check_email' );
    
    function email_already_in_db ( $result, $tags ) {
        // Retrieve the posted form
        $form  = WPCF7_Submission::get_instance();
        $form_posted_data = $form->get_posted_data();
    
        // Get the field name that we want to check for duplicates.
        // I added 'unique' to the beginning of the field name in CF7
        // Checking for that with preg_grep
        $unique_field_name = preg_grep("/unique(\w+)/", array_keys($form_posted_data));
    
        // $unique_field_name comes back as array so the next three lines give us the key as a string
        reset($unique_field_name);
        $first_key = key($unique_field_name);
        $unique_field_name = $unique_field_name[$first_key];
    
        // Check the form submission unique field vs what is already in the database
        $email = $form->get_posted_data($unique_field_name);
        global $wpdb;
        $entry = $wpdb->get_results( "SELECT * FROM wp_cf7_vdata_entry WHERE name LIKE '$unique_field_name' AND value='$email'" );
    
        // If already in database, invalidate
        if (!empty($entry)) {
          $result->invalidate($field_name, 'Your email: '.$email.' already exists in our database.');
          }
        // return the filtered value
      return $result;
    }

    https://stackoverflow.com/questions/29504337/how-do-i-check-if-email-submitted-via-contact-form-7-exists-in-my-database

Viewing 1 replies (of 1 total)
  • Plugin Author Vsourz Digital

    (@vsourz1td)

    Hi @laksoner ,

    Hope you are well.

    I do agree with your concerns and we have already considered “prevent duplicate submissions” feature development in our plugin development pipeline.

    Regarding to your solution code, it seems like the function ’email_already_in_db’ looks proper and will work as intended. I believe, you are using this code/function in your project after altering your WordPress environment related values/fields. values/fields like ‘Database table prefix’, ‘CF7 form field name/prefix’,etc. If this all correct then i think this code should works.

    I am not sure how you planing to show duplicate submissions error messages to your customer but you may need to choose Action/Filter accordingly. as per my opinion, you can use ‘wpcf7_validate’ filter as well.

    Regards,

Viewing 1 replies (of 1 total)
  • The topic ‘Check if field value exists / prevent duplicate submissions’ is closed to new replies.