• I need to block form submission if $_POST variable does not exist. I am using wordpress Shortcode (that I created) inside of the Contact Form 7 form to output fields that I need validated. It’s a simple field, just a multiple checkbox field (did not think I need to create a custom field in Contact Form 7 to be able to handle validation of this).

    I am using the wpcf7_form_elements filter like so:
    add_filter('wpcf7_form_elements', 'do_shortcode');

    Everything is working and even on wpcf7_submit hook I am able to determine if the $_POST[‘brochures’] array exists or not, even with the form being sent via AJAX.

    The only problem I’m facing is that I need to be able to say there was an error, and stop form submission if the $_POST[‘brochures’] variable is empty. Using the validation hooks do not seem to work since they are referencing tags, and causes error on get_invalid_fields(), even if I set $result[‘invalid_fields’] array manually. I need to just stop form submission and output error message. The field does not exist in wpcf7, since it was added via Shortcode inside the Contact Form post type. So, if there is no way to just output error on validation of the $_POST[‘brochures’] array, on whether or not it exists, than is there a way to create the fields dynamically in code?

    function catalogs_wpcf7_submit($instance, $result)
    {
    	if ($instance->name() == 'catalogs-brochures')
    	{
    		$get_props = $instance->get_properties();
    		file_put_contents(ABSPATH . '/wpcf7_submit_properties.txt', "Get Properties\n" . var_export($get_props, true) . PHP_EOL, FILE_APPEND || LOCK_EX);
    
    		// Get the Brochures that we sent!
    		if (!empty($_POST['brochures']))
    		{
    			$brochures = implode(', ', $_POST['brochures']);
    			file_put_contents(ABSPATH . '/wpcf7_brochures.txt', $brochures, FILE_APPEND || LOCK_EX);
    		}
    		else
    		{
    			// What to do here?  Or do I need another filter to handle this?
    		}
    	}
    }
    add_action( 'wpcf7_submit', 'catalogs_wpcf7_submit', 10, 2 );

    https://wordpress.org/plugins/contact-form-7/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘How to stop form from sending data if $_POST[] variable does not exist’ is closed to new replies.