Viewing 11 replies - 1 through 11 (of 11 total)
  • Thread Starter giathienhp

    (@giathienhp)

    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”;
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
    $found = true;
    }
    return $found;
    }

    function my_validate_email($result, $tag) {
    $formName = ’email_form’; // Name of the form containing this field
    $fieldName = ’email_123′; // Set to your form’s unique field name
    $name = $tag[‘name’];
    if($name == $fieldName){
    $valueToValidate = $_POST[$name];
    if (is_already_submitted($formName, $fieldName, $valueToValidate)) {
    $result[‘valid’] = false;
    $result[‘reason’][$name] = ‘Email has already been submitted’; // error message
    }
    }
    return $result;
    }

    add_filter(‘wpcf7_validate_email*’, ‘my_validate_email’, 10, 2);

    http://cfdbplugin.com/?page_id=904

    Plugin Author Michael Simpson

    (@msimpson)

    In function is_already_submitted set:
    $atts['show'] = "submit_time,$fieldName";
    (comma-delimited list of fields, just like show in short codes).

    Then you need to return that to the calling my_validate_email function and put the value into $result['reason'][$name] as the error message for the user.

    In is_already_submitted, do not return from inside the ‘while’ loop.

    Hi Michael,
    I am trying to implement this code to allow user send booking form just once. Ideally would be hide form from users who already subbmited it (based od username) but cannot fugure it out.
    I used this code:

    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'] = "submit_time,$fieldName";
    $atts['filter'] = "$fieldName=$fieldValue";
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
    $found = true;
    }
    return $found;
    }
    
    function my_validate_email($result, $tag) {
    $formName = 'test2'; // Name of the form containing this field
    $fieldName = 'your-email2369'; // Set to your form's unique field name
    $name = $tag['name'];
    if($name == $fieldName){
    $valueToValidate = $_POST[$name];
    if (is_already_submitted($formName, $fieldName, $valueToValidate)) {
    $result['valid'] = false;
    $result['reason'][$name] = 'Email has already been submitted'; // error message
    }
    }
    return $result;
    }
    
    add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);

    and after submitting for I can only see spinner all the time.
    Is there something you can help me with please.

    xx
    Anika

    Plugin Author Michael Simpson

    (@msimpson)

    http://cfdbplugin.com/?page_id=904

    change

    $result['valid'] = false;
    $result['reason'][$name] = 'Email has already been submitted'; // error message

    to

    $result->invalidate($name, 'Email has already been submitted'); // error message

    to be compatible with the most recent CF7.

    But there might be some other problem going on that is causing an error and therefore the spinner not to go away.

    Michael, thank a lot for your help. Unfortunately it did not help. Without this code form works fine. When I paste it to functions it starts spinning:(
    xx
    Anika

    Plugin Author Michael Simpson

    (@msimpson)

    One of the lines must be giving an error. Maybe $_POST[$name] fails when the field is not present….you could try removing most of the code, get it to not give the error, then start adding lines back in.

    Hi Michael,
    Thanks a lot. I am not a specialist, just amateur and my modifications only cause fatal errors. Anyway, thanks for your help.
    xx
    Anika

    add_filter( ‘wpcf7_validate’, ’email_already_in_db’, 10, 2 );

    function email_already_in_db ( $result, $tags ) {
    // retrieve the posted email
    $form = WPCF7_Submission::get_instance();
    $email = $form->get_posted_data(’email’);
    // if already in database, invalidate
    if( is_already_submitted(‘Contact form 1′ ,’email’,$email ) )
    $result->invalidate(’email’, ‘Your email exists in our database’);
    // return the filtered value
    return $result;
    }

    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”;
    $exp->export($formName, $atts);
    $found = false;
    while ($row = $exp->nextRow()) {
    $found = true;
    }
    return $found;
    }

    Just set the field name, and form name in above function.

    Does this code also supports the prevention of duplicate phone numbers..?

    You need to customize the email_already_in_db function as per your requirement or make common function and use condition for validate the fields as much you want.

    If you want that please let me know.

    yes please kindly, i’ll be very much grateful to you.

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