WordPress.org

Ready to get started?Download WordPress

Forums

Fast Secure Contact Form
[resolved] Check email field in the db for error (using CF to DB ext) (20 posts)

  1. lobinof
    Member
    Posted 2 years ago #

    I'm building a form (FSCF and Contact Form to DB extension) with a question for a competition and I do not want multiple answers with the same email in the db.

    Is it possible to check if the email field in the form exist inside the db when submitting?
    For example, I want that the form to check the presence of the email in the db, if the email exist it should show error "this email is just in our db".

    http://wordpress.org/extend/plugins/si-contact-form/

  2. Michael Simpson
    Member
    Posted 2 years ago #

    Unfortunately no. There is no way to validate data against what is in the DB.

  3. Daniele
    Member
    Posted 1 year ago #

    Don't know if someone is interested in this isssue any more, but I've find a way to make data validation against db submissions.

    Thanks to this post, I've created a custom email validation function to avoid customers of one of my sites, to request more than one discount coupon with the same email address.

    PLEASE NOTE: there's no way to apply this custom validation to a particular form (i.e form id) cause the form id is not a parameter we can check using those validation filters.
    So, if you have more then one form and you want to apply the custom validation only to a particular form, the only way I know is to use a univocal field name, or programmaticaly create a new type of field to use in your form and for your custom validation.
    Obviously, every form using that field-id or that field-name, will make use of the custom validation function.

    So, here is my code:

    // Add custom validation for CF7 form fields
    function is_coupon_already_requested($email){ // Check if a customer has already request a discount coupon
    	global $wpdb;
    	define ('FORM_NAME', 'Coupon');
    	define ('FIELD_NAME', 'coupon_email');
    	$query="SELECT
    			submit_time,
    				if(field_name='".FIELD_NAME."', field_value, null ) AS '".FIELD_NAME."'
    		FROM ss_cf7dbplugin_submits
    		WHERE form_name = '".FORM_NAME."' AND field_value='".$email."'
    		";
    	$myrows = $wpdb->get_results( $query, ARRAY_A );
    	if (is_array($myrows) && count($myrows)){
    		return true; //email already exists in the db
    	}else{
    		return false; // first time with this email
    	}
    }
    function duplicate_email_validation_filter($result,$tag){
    	define ('FIELD_NAME', 'coupon_email');
    	$type = $tag['type'];
    	$name = $tag['name'];
    	if($name == FIELD_NAME){ // Only apply to fields with the form field name of "coupon_email"
    		$the_value = $_POST[$name];
    		if(is_coupon_already_requested($the_value)){ //An user with this email has already received a coupon)
    			$result['valid'] = false;
    			$result['reason'][$name] = 'Email address already used.';
    		}
    	}
    	return $result;
    }
    add_filter('wpcf7_validate_email','duplicate_email_validation_filter', 10, 2); // Email field
    add_filter('wpcf7_validate_email*', 'duplicate_email_validation_filter', 10, 2); // Req. Email field

    Hope it can be useful

  4. Michael Simpson
    Member
    Posted 1 year ago #

    You may find the API for accessing form data easier to use than writing SQL to find existing form entries in the DB.

  5. Daniele
    Member
    Posted 1 year ago #

    Thanks Michael, I wasn't aware of this API. Great support, as usual!

  6. Daniele
    Member
    Posted 1 year ago #

    Well, just for the sake of completeness, here is the same code but using Michael's API to access form data in the db:

    // Add custom validation for CF7 form fields
    function is_coupon_already_requested($email){ // Check if a customer has already request a discount coupon
    	define ('FORM_NAME', 'Coupon');
    	define ('FIELD_NAME', 'coupon_email');
    	if (empty($email)) return false;
    	$findit=false;
    	require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBFormIterator.php');
    	$exp = new CFDBFormIterator();
    	$exp->export(FORM_NAME, array('show' => FIELD_NAME, 'search' => $email));
    	while ($row = $exp->nextRow()) {
    		$findit=true;
    	}
    	return $findit;
    }
    function duplicate_email_validation_filter($result,$tag){
    	define ('FIELD_NAME', 'coupon_email');
    	$type = $tag['type'];
    	$name = $tag['name'];
    	if($name == FIELD_NAME){ // Only apply to fields with the form field name of "coupon_email"
    		$the_value = $_POST[$name];
    		if( !empty($the_value) && is_coupon_already_requested($the_value)){ //An user with this email has already received a coupon)
    			$result['valid'] = false;
    			$result['reason'][$name] = 'Email address already used.';
    		}
    	}
    	return $result;
    }
    add_filter('wpcf7_validate_email','duplicate_email_validation_filter', 10, 2); // Email field
    add_filter('wpcf7_validate_email*', 'duplicate_email_validation_filter', 10, 2); // Req. Email field

    N.B.: Please note that if you pass an empty string as search value to the API, it returns all unfiltered rows for that query.
    This is the reason why I added a check to see if the email field is empty or not.

  7. stcc
    Member
    Posted 1 year ago #

    Thank you very much for the codes! I am wondering how I should use these codes: do I put them in the function.php in the theme folder? how should I call it in the wordpress page/post so that it will work (and check if there are duplicates in the database for that particular field). I will greatly appreciate if you can give me some suggestions. Thank you very much!

  8. Daniele
    Member
    Posted 1 year ago #

    @stcc
    Sure, you have to add the code above in your function.php theme file or create one if your theme doesn't already include it, and put the code between the open and close php tags:

    <?php
    //code here
    ?>

    Change the relevant data in the code (form name, field name, error message etc) and it should work as is.

  9. stcc
    Member
    Posted 1 year ago #

    Hi Daniele:

    Thank you so much for your quick reply! I have put your code in the function.php (in the theme folder), and have modified the form name, field name, error message etc according to my database structure. However, when I tried to submit the entry from my form, why it still takes/adds the duplicate entry into the database although the same entry is already in the database? I am a little bit confused so I wonder if that is the steps I should follow:

    change your codes according to my formname/field
    put the code in the function.php
    then I am all set to test the form entry?

    Please correct me if I am wrong. Thank you so much!

  10. stcc
    Member
    Posted 1 year ago #

    By the way, if I code the above codes to the function.php file, I can just add them at the end of the function.php file, and I don't need to put the open and close php tags, right? (only if I create a new php file to put the codes in, I will need the open and close php tags)? Thank you!

  11. stcc
    Member
    Posted 1 year ago #

    Here are the codes I slightly modified based on your code:

    function is_clicker_already_returned($myclicker){ // Check if a clicker already returned
    global $wpdb;
    define ('FORM_NAME', 'Clickers Inventory');
    define ('FIELD_NAME', 'Clicker');
    $query="SELECT
    submit_time,
    if(field_name='".FIELD_NAME."', field_value, null ) AS '".FIELD_NAME."'
    FROM wp_cf7dbplugin_submits
    WHERE form_name = '".FORM_NAME."' AND field_value='".$myclicker."'
    ";
    $myrows = $wpdb->get_results( $query, ARRAY_A );
    if (is_array($myrows) && count($myrows)){
    return true; //clicker already exists in the db
    }else{
    return false; // first time with this email
    }
    }
    function duplicate_clicker_validation_filter($result,$tag){
    define ('FIELD_NAME', 'Clicker');
    $type = $tag['type'];
    $name = $tag['name'];
    if($name == FIELD_NAME){ // Only apply to fields with the form field name of "clicker"
    $the_value = $_POST[$name];
    if(is_clicker_already_returned($the_value)){ //An clicker with this barcode has already been returned)
    $result['valid'] = false;
    $result['reason'][$name] = 'clicker already returned.';
    }
    }
    return $result;
    }
    add_filter('wpcf7_validate_clicker','duplicate_clicker_validation_filter', 10, 2); // Email field
    add_filter('wpcf7_validate_clicker*', 'duplicate_clicker_validation_filter', 10, 2); // Req. Email field

  12. Daniele
    Member
    Posted 1 year ago #

    @sttc

    Please edit the post and mvoe your code to a place like pastebin.com, then post a link to the code.

    The steps you've listed are ok. Double check function name: name used in the add_filter call must match with the defined function name.
    If it seems all ok, there should be some erro in the logic of your code. Just to start, try to invert the condition you are using to check for duplicates, to see if something happens or use firefox+firebug to debug.

  13. stcc
    Member
    Posted 1 year ago #

    here is the link for the above code:

    http://pastebin.com/Ev1CSrfC

    Thank you!

  14. stcc
    Member
    Posted 1 year ago #

    Logically it seems right for me. I am not sure if there are syntax errors inside the codes.... very confused by it not working right....

  15. Daniele
    Member
    Posted 1 year ago #

    The code seems ok.
    Please verify the name of the form and the name of the field.
    For example, if the code generated by cf7 for the field is
    [email* coupon_email 42/ id:email class:inputfield class:email]
    the name you have to use is "coupon_email"

    And if the shortcode you are using to render the form in your post(s)/page(s) is
    [contact-form-7 id="396" title="Coupon Request"]
    the form name you have to use is "Coupon Request", as in the title parameter.

  16. stcc
    Member
    Posted 1 year ago #

    Hi Daniele:

    You have written very great and useful codes.
    The field name and the form title are all correct. ...
    I am not sure why it is not working. So confused.
    Thank you very much!

  17. Daniele
    Member
    Posted 1 year ago #

    Is the page with form public? Can you pot the url?

  18. stcc
    Member
    Posted 1 year ago #

    Hi Daniele:

    The form is not public. However I can give you access to it. I can email you the user name/password if you don't mind to give me your contact information. Please email me at: leeou76@yahoo.com

    I really appreciate your kind help!

  19. walaan
    Member
    Posted 1 year ago #

    I try this code after change form name .. ets ,
    But it doesn't work with me :(

    any help ?

  20. walaan
    Member
    Posted 1 year ago #

    I get it , and it is work now : )

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic