Using on_sent_ok

Introduction

Explain how CF7 users can use on_sent_ok to implement some commonly requested extra features to CF7. Cover how this is done by adding JavaScript code. Very powerful but also can cause problems.

Before you begin

Learn basic JavaScript – Codecademy

List of on_sent_ok JavaScript Tools

Collect list of commonly requested things from CF7 Support Forum.

Redirect to new page

Reset form

Google Events Tracking

 

From CF7 Documentation

on_sent_ok: “alert(‘sent ok’);”
on_submit: “alert(‘submit’);”
If you set on_sent_ok: followed by a one-line JavaScript code, you can tell the contact form the code that should be performed when the mail is sent successfully. Likewise, with on_submit:, you can tell the code that should be performed when the form submitted regardless of the outcome.

on_sent_ok: “location = ‘http://example.com/’;”
The simplest way is using on_sent_ok JavaScript action hook. By using this hook, you can specify a JavaScript code that you wish to run after the form is successfully submitted. You will find the Additional Settings field at the bottom of the contact form management page. It is a JavaScript hook, so it needs JavaScript to work. You’ll need another hack if you need to redirect on non-JavaScript environments.

See http://contactform7.com/additional-settings/
See http://contactform7.com/redirecting-to-another-url-after-submissions/

Other

See http://kb.cf7skins.com/javascript-conflict-problems/#on_sent_ok

Notes
Using on_sent_ok can cause problems

I have gotten cautious about the use of on_sent_ok as it easily causes problems when not used appropriately. I am not sure about how I have used it.

While on_sent_ok can cause problems when not used appropriately, it makes sense to use it for simple redirection.

The main issue with on_sent_ok is that the parameter has to be suitable JavaScript code. Many of the issues occur when people with little of no JavaScript skills use copy-pasta solutions for complex JavaScript stuff like interacting with Google Analytics.

Debugging
Short example, form:
[select menu-23 “this” “other”]
[checkbox checkbox-357 “one” “two”]

At additional settings:
on_sent_ok: “console.log( jQuery( ‘select[name=\”menu-23\”]’ ).val() ); console.log( jQuery( ‘input[name=\”checkbox-357[]\”]:checked’ ).val() );”
After sending check your browser console.

Controlling Form Spam in Contact Form 7

Articles

Plugins

Docs

Using the Contact Form DB plugin with your CF7 forms

My experience is that the submission storage plugins are very robust and rarely fail to save a CF7 form submit.

They are not however always a good indication of CF7 sending emails. The green “Your message was sent successfully” message is a much clearer indicator that CF7 is sending the email.

The submission storage plugins appear to save a CF7 form submit even if the email is not sent due to a Javascript Conflict or unallowed From: email address.

This is partly why I think they are so often used by experienced WP users – they provide a robust record of all form submissions even when things go a little wrong.

Using the Contact Form 7 Dynamic Text Extension plugin

http://wordpress.org/plugins/contact-form-7-dynamic-text-extension/

Contact Form 7 is an excellent WordPress plugin, and the CF7 DTX Plugin makes it even more awesome by adding dynamic content capabilities. While default values in Contact Form 7 are static. CF7 DTX lets you create pre-populated fields based on other values. Some examples might include:

Auto-filling a URL
Auto-filling a Post ID, title, or slug
Pre-populating a Product Number
Referencing other content on the site
Populating with post info
Populating with user info
Populating with custom fields
Any value you can write a shortcode for

There are many more case-specific examples. I searched for a solution, and there are some decent hacks out there. Many of them are explored in this forum topic: Contact Form 7 Input Fields Values as PHP Get-Viarables. However, they all involved hacking the current Contact Form 7 code, which means next time the plugin is updated their edits will be overwritten. That’s bad.

This Dynamic Text Extension plugin provides a more elegant solution that leaves the Contact Form 7 Plugin intact.

See also http://sevenspark.com/wordpress-plugins/how-to-dynamically-set-the-recipient-to-email-address-in-contact-form-7

See also http://sevenspark.com/tutorials/how-to-create-a-counter-for-contact-form-7

See also http://sevenspark.com/code/how-to-create-a-dynamic-wordpress-contact-form

Better Contact Form 7 Checkboxes & Radio Buttons

Article explains use of important options available for Checkboxes & Radio Buttons in Contact Form 7.

Include example of completed CF7 tag with option.

See http://contactform7.com/checkboxes-radio-buttons-and-menus/

Checkbox & Radio Button Tag Syntax

See http://contactform7.com/tag-syntax/#form_tag

Setting Default Options

default:(num) default:2
default:1_2_3 Pre-checked options. The integer following default: is the position in the order of items. 1 is the first one. If you want to make two or more items selected, joint integers with underbar (_), as default:1_2_3.

Make checkboxes exclusive

exclusive Make checkboxes exclusive. I mean, when you check a checkbox in a group, others are unchecked, so it accepts zero checked or one checked. This is a JavaScript gimmick, so requires JavaScript. Radio button doesn’t support this option.

Add other field to checkboxes & radio buttons

free_text Append a free input text field to last item.

Layout Options for Checkboxes and Radio Buttons

label_first By default, a checkbox or a radio button are put first, and a label last. By adding label_first option, you can reverse them.

See also http://contactform7.com/custom-layout-for-checkboxes-and-radio-buttons/

Contact Form 7 Showcase

Please share links to great CF7 forms. Can be you own work or others. Aim is to highlight what you can achieve with CF7 (if you are skilled at HTML & CSS).

Examples

http://badwolfsyndicate.com/idea-submission/
http://buzztone.com.au/web-design-process/quote/

Submit Contact Form 7 Form

If you would like to submit a for inclusion in the , please complete the form below.

Error: Contact form not found.


Of course this form is a Contact Form 7 Form – use Firebug to see how we’ve built it!

Back to Styling Contact Form 7 Forms

CF7 CSS Classes

For CF7 forms we have several ids & classes which can be used to ensure that our CSS changes don’t inadvertantly effect other elements on your website.

 
class="wpcf7":
 
<div id="wpcf7-f800-p709-o1" class="wpcf7">
 
div.wpcf7 {  /* ../contact-form-7/includes/css/styles.css */
	margin: 0;
	padding: 0;
}
 
class="wpcf7-form":
 
<form class="wpcf7-form" novalidate="novalidate" method="post" 
action="/ONLINE/CF7/cf7skinsPoC/styles/premium/cagney/#wpcf7-f800-p709-o1">
 
class="wpcf7-form-control":
 
<span class="wpcf7-form-control wpcf7-checkbox">
 
class="wpcf7-form-control-wrap":
 
<span class="wpcf7-form-control-wrap checkbox-0">
 
span.wpcf7-form-control-wrap {  /* ../contact-form-7/includes/css/styles.css */
	position: relative;
}
 
class="wpcf7-text":
 
<input id="cf7s-item1a" class="wpcf7-form-control wpcf7-text wpcf7-validates-as-required
cf7s-textfield" type="text" placeholder="default" aria-required="true" maxlength="100"
size="60" value="" name="cf7s-text-01">
 
class="wpcf7-textarea":
 
<textarea class="wpcf7-form-control wpcf7-textarea" rows="10" cols="40" name="cf7s
textarea-01"></textarea>
 
class="wpcf7-email":
 
<input class="wpcf7-form-control wpcf7-text wpcf7-email wpcf7-validates-as-email"
type="email" size="40" value="" name="cf7s-email-01">
 
class="wpcf7-url":
 
<input class="wpcf7-form-control wpcf7-text wpcf7-url wpcf7-validates-as-url" type="url"
size="40" value="" name="cf7s-url-01">
 
class="wpcf7-tel":
 
<input class="wpcf7-form-control wpcf7-text wpcf7-tel wpcf7-validates-as-tel" type="tel"
size="15" value="" name="cf7s-tel-01">
 
class="wpcf7-number":
 
<input class="wpcf7-form-control wpcf7-number wpcf7-validates-as-number" type="number"
value="" name="cf7s-number-01">
 
class="wpcf7-range":
 
<input class="wpcf7-form-control wpcf7-range wpcf7-validates-as-number" type="range"
value="" name="cf7s-range-01">
 
class="wpcf7-date":
 
<input class="wpcf7-form-control wpcf7-date wpcf7-validates-as-date" type="date" 
value="" name="cf7s-date-01">
 
class="wpcf7-file":
 
<input class="wpcf7-form-control wpcf7-file" type="file" size="40" value="1" 
name="cf7s-file-01">
 
class="wpcf7-quiz":
class="wpcf7-quiz-label":
 
<span class="wpcf7-quiz-label">12+48=?</span><input class="wpcf7-form-control wpcf7-quiz" 
type="text" aria-required="true" size="40" name="cf7s-quiz-01"><input type="hidden" 
value="1c7a729e1466603f91d3595d6afcac26" name="_wpcf7_quiz_answer_cf7s-quiz-01">
 
class="wpcf7-acceptance":
 
<input class="wpcf7-form-control wpcf7-acceptance" type="checkbox" value="1" name="cf7s 
acceptance-01">
 
class="wpcf7-captchac":
 
<img class="wpcf7-form-control wpcf7-captchac wpcf7-captcha-cf7s-captcha-01" width="72" 
height="24" src="http://localhost/ONLINE/CF7/cf7skinsPoC/wp-content/images/wpcf7_captcha
/1692768786.png" alt="captcha">
 
class="wpcf7-captchar":
 
<span class="wpcf7-form-control-wrap cf7s-captcha-01"><input class="wpcf7-form-control 
wpcf7-captchar" type="text" size="40" value="" name="cf7s-captcha-01"></span>
 
class="wpcf7-list-item":
 
<span class="wpcf7-list-item">
 
span.wpcf7-list-item {  /* ../contact-form-7/includes/css/styles.css */
	margin-left: 0.5em;
}
 
class="wpcf7-list-item-label":
 
<span class="wpcf7-list-item-label">Lieber.gleich.richtig.® Imagemappe</span>
 
class="wpcf7-checkbox":
 
<span class="wpcf7-form-control wpcf7-checkbox">
 
class="wpcf7-radio":
 
<span class="wpcf7-form-control wpcf7-radio">
 
class="wpcf7-select":
 
<select class="wpcf7-form-control wpcf7-select" multiple="multiple" name="cf7s-menu-01[]">
 
class="wpcf7-submit":
 
<input class="wpcf7-form-control wpcf7-submit" type="submit" value="Senden">

Back to Styling Contact Form 7 Forms

Default CF7 CSS style

The Contact Form 7 plugin uses very minimal CSS styling to allow CF7 forms to blend in with most websites.

Default CF7 CSS style:

div.wpcf7 {
	margin: 0;
	padding: 0;
}
 
div.wpcf7-response-output {
	margin: 2em 0.5em 1em;
	padding: 0.2em 1em;
}
 
div.wpcf7-mail-sent-ok {
	border: 2px solid #398f14;
}
 
div.wpcf7-mail-sent-ng {
	border: 2px solid #ff0000;
}
 
div.wpcf7-spam-blocked {
	border: 2px solid #ffa500;
}
 
div.wpcf7-validation-errors {
	border: 2px solid #f7e700;
}
 
span.wpcf7-form-control-wrap {
	position: relative;
}
 
span.wpcf7-not-valid-tip {
	position: absolute;
	top: 20%;
	left: 20%;
	z-index: 100;
	background: #fff;
	border: 1px solid #ff0000;
	font-size: 10pt;
	width: 280px;
	padding: 2px;
}
 
span.wpcf7-not-valid-tip-no-ajax {
	color: #f00;
	font-size: 10pt;
	display: block;
}
 
span.wpcf7-list-item {
	margin-left: 0.5em;
}
 
.wpcf7-display-none {
	display: none;
}
 
div.wpcf7 img.ajax-loader {
	border: none;
	vertical-align: middle;
	margin-left: 4px;
}
 
div.wpcf7 .watermark {
	color: #888;
}

Back to Styling Contact Form 7 Forms