{"id":904,"date":"2013-02-22T22:47:56","date_gmt":"2013-02-23T03:47:56","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=904"},"modified":"2016-01-27T16:47:06","modified_gmt":"2016-01-27T21:47:06","slug":"preventing-duplicate-submissions-from-cf7","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=904","title":{"rendered":"Preventing Duplicate Submissions from CF7"},"content":{"rendered":"<p><strong>Situation<\/strong>: You have users who submit forms but you don&#8217;t want them to submit more than once.<\/p>\n<p><strong>Solution<\/strong>: add a custom validation to a form field that uniquely identifies the submission.<\/p>\n<p>This solution is for Contact Form 7. Contact Form 7 has a hook into which you can inject custom validation code. It is somewhat limited, so read carefully. (This solution is based on: <a href=\"http:\/\/contactform7.com\/2015\/03\/28\/custom-validation\/\" target=\"_blank\">Contact Form 7 Custom Validation<\/a>)<\/p>\n<p>Let&#8217;s assume that essentially what you need to do is to prevent users from submitting a form more than once with the same email address in it. In this example, we will put a validation on one particular form field and check if that email was submitted previously. We will ignore all other fields in this example.<\/p>\n<p>The limitation to the\u00a0Contact Form 7 field validation is that although we can put in code to validate a particular field, in that code we can&#8217;t know what form the submission is coming from. This means that if you set up a validation on a field named &#8220;email&#8221; then that validation will be run against all form submissions that have an &#8220;email&#8221; field. If you have more than one form with a field named &#8220;email&#8221; but you\u00a0only want this validation run for one particular form, then you have a problem.<\/p>\n<p>The best idea is to give a unique name to the email field in your form. Use this name only on this form and none other. In this example, we use &#8220;email_123&#8221;. The Contact Form 7 form definition for this example is:<br \/>\nForm Name: <strong>email_form<\/strong><\/p>\n<pre lang=\"html\" line=\"1\" escaped=\"true\">\r\n<p>Your Name (required)<br \/>\r\n    [text* name] <\/p>\r\n\r\n<p>Your Email (required)<br \/>\r\n    [email* email_123] <\/p>\r\n\r\n<p>[submit \"Send\"]<\/p><\/pre>\n<p>To create the validation, we add WordPress filter code into <strong>Tools<\/strong> -&gt; <strong>Shortcodes Actions and Filters<\/strong> using the <a href=\"https:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\" target=\"_blank\">Shortcodes Actions and Filters<\/a> plugin. This is the same technique used in <a href=\"http:\/\/cfdbplugin.com\/?page_id=747\" target=\"_blank\">Changing Form Data Before it is Saved<\/a>.<\/p>\n<p>The following code is an example. You will need to make some changes to make it work for you. In the <strong>my_validate_email function<\/strong>:<\/p>\n<ul>\n<li><span style=\"line-height: 13px;\">Change <strong>$formName<\/strong> to the name of your form<\/span><\/li>\n<li>Change <strong>$fieldName<\/strong> to the name of your email field<\/li>\n<li>Change <strong>$errorMessage<\/strong> an error message you like<\/li>\n<\/ul>\n<p>NOTE: this code is now updated to work with <a href=\"http:\/\/contactform7.com\/2015\/01\/06\/contact-form-7-41-beta\/\" target=\"_blank\">changes made in CF7 version 4.1<\/a>.<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">\r\n\/**\r\n * @param $formName string\r\n * @param $fieldName string\r\n * @param $fieldValue string\r\n * @return bool\r\n *\/\r\nfunction is_already_submitted($formName, $fieldName, $fieldValue) {\r\n    require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBFormIterator.php');\r\n    $exp = new CFDBFormIterator();\r\n    $atts = array();\r\n    $atts['show'] = $fieldName;\r\n    $atts['filter'] = \"$fieldName=$fieldValue\";\r\n    $atts['unbuffered'] = 'true';\r\n    $exp->export($formName, $atts);\r\n    $found = false;\r\n    while ($row = $exp->nextRow()) {\r\n        $found = true;\r\n    }\r\n    return $found;\r\n}\r\n\r\n\/**\r\n * @param $result WPCF7_Validation\r\n * @param $tag array\r\n * @return WPCF7_Validation\r\n *\/\r\nfunction my_validate_email($result, $tag) {\r\n    $formName = 'email_form'; \/\/ Change to name of the form containing this field\r\n    $fieldName = 'email_123'; \/\/ Change to your form's unique field name\r\n    $errorMessage = 'Email has already been submitted'; \/\/ Change to your error message\r\n    $name = $tag['name'];\r\n    if ($name == $fieldName) {\r\n        if (is_already_submitted($formName, $fieldName, $_POST[$name])) {\r\n            $result->invalidate($tag, $errorMessage);\r\n        }\r\n    }\r\n    return $result;\r\n}\r\n\r\n\/\/ use the next line if your field is a **required email** field on your form\r\nadd_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);\r\n\/\/ use the next line if your field is an **email** field not required on your form\r\nadd_filter('wpcf7_validate_email', 'my_validate_email', 10, 2);\r\n\r\n\/\/ use the next line if your field is a **required text** field\r\nadd_filter('wpcf7_validate_text*', 'my_validate_email', 10, 2);\r\n\/\/ use the next line if your field is a **text** field field not required on your form \r\nadd_filter('wpcf7_validate_text', 'my_validate_email', 10, 2);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Situation: You have users who submit forms but you don&#8217;t want them to submit more than once. Solution: add a custom validation to a form field that uniquely identifies the submission. This solution is for Contact Form 7. Contact Form 7 has a hook into which you can inject custom validation code. It is somewhat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1250,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-904","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-eA","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=904"}],"version-history":[{"count":27,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/904\/revisions"}],"predecessor-version":[{"id":1386,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/904\/revisions\/1386"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1250"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}