'Contactform7 prevent duplicate field value submission
I am using WordPress 3.8 and contact form 7 plugin with contact form 7 db extension.
I want to check for existing email which I do on submit on a hook (alter_wpcf7_posted_data) in functions.php as below:
function alter_wpcf7_posted_data( $data ) {
global $wpcf7;
if(email_exists( $_POST['mail'])) {
$data = array();
}
return $data;
}
add_filter("wpcf7_posted_data", "alter_wpcf7_posted_data");
This hook throws me an error on the source but does not save data.
Basically, I wish not to save the data and throw a validation error on the form if email_exists() returns true.
Does anyone know how to prevent form submission.
Note: I am not using an AJAX form submission.
Solution 1:[1]
I find solution for this. Just add this code in your function.php
/**
* @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);
remember to change email_form with your contact form name and email_123 with email field name. Work just fine for me with WordPress 4.9.5 and CF7 5.0.1
Solution 2:[2]
I have tried many solutions but many not working with me finally decide to change button content and backgroup-color for 10 second (or whatever you need)
have to set ID for submit button
[submit id:SendFormDataM "send your request"]
they use this jquery code (I have added it in jquery.jvcf7_validation.js file)
function submitPoll(){
document.getElementById("SendFormDataM").style.backgroundColor = "#000000";
document.getElementById("SendFormDataM").value="please waiting";
setTimeout(function() {
document.getElementById("SendFormDataM").style.backgroundColor = "#bc8a49";
document.getElementById("SendFormDataM").value="send your request";
}, 10000);
}
var el = document.getElementById("SendFormDataM"); // use this if you have multi ID's
if(el){
el.addEventListener('click', submitPoll);
}
this will change background-color to black and write "please wait"
after 10 seconds, the button will be back to the normal background-color with old content
I have tried disabled the button, but its didn't send form data
Solution 3:[3]
After a long time trying to find a functional code that can be used independently of the plugin that saves contacts in the database, I came up with the code below.
/*We created the filter*/
add_filter( 'wpcf7_validate', 'email_already_in_db', 10, 2 );
/*We created the function*/
function email_already_in_db ( $result, $tags ) {
// We take the information from the form being submitted
$form = WPCF7_Submission::get_instance(); /*Here is the form ID of the Contact Form*/
$email = $form->get_posted_data('email'); /*Here is the email field*/
date_default_timezone_set('America/Sao_Paulo'); /*We set the time zone*/
$datetoday = date("Y-m-d"); /*We take the current date in the format that the contact plugin records the date*/
global $wpdb;
/*We make a select in the table where the contacts are recorded, checking if the email informed already exists on today's date */
$entry = $wpdb->get_results( "SELECT * FROM wp_db7_forms WHERE form_value LIKE '%$email%' AND form_date LIKE '%$datetoday%'" );
// If the select query in the database is positive (the email exists on the current date), it returns the error in the form, in the email field, not sending
if (!empty($entry)) {
$result->invalidate('email', 'Email already exists');
}
return $result;
}
I built my version based on: https://www.stacknoob.com/s/6X5Lisxm3DE87aGby3NzQZ
Solution 4:[4]
I Created my own solution of the question.. Just replace the
DB_PREFIXandFORM_IDaccording to your form ...Check it and I hope this will help you
function is_already_submitted($formPostId, $fieldName, $fieldValue) {
global $wpdb;
/*We make a select in the table where the contacts are recorded, checking if the email informed already exists on today's date */
$entry = $wpdb->get_results( "SELECT * FROM DB_PREFIX_db7_forms WHERE form_value LIKE '%$fieldValue%' AND form_post_id = '$formPostId'" );
// If the select query in the database is positive (the email exists on the current date), it returns the error in the form, in the email field, not sending
$found = false;
if (!empty($entry)) {
$found = true;
}
return $found;
}
function my_validate_email($result, $tag) {
$formPostId = 'FORM_ID'; // Change to name of the form containing this field
$fieldName = 'your-email'; // Change to your form's unique field name
$errorMessage = 'This email address is already registered'; // Change to your error message
$name = $tag['name'];
if ($name == $fieldName) {
if (is_already_submitted($formPostId, $fieldName, $_POST[$name])) {
$result->invalidate($tag, $errorMessage);
}
}
return $result;
}
add_filter('wpcf7_validate_email*', 'my_validate_email', 10, 2);
Solution 5:[5]
Pay attention to related CF7 plugins. In my case the duplicate form submission was caused by Jquery Validation For Contact Form 7.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zoe stands with Ukraine |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | CodingEra |
| Solution 5 | Andrei - Lucian Cirjan |
