cforms ii custom submit script
Aug 15, 2008 in php, wordpress
Cforms ii is one of the most powerful plugins available for wordpress. It enables quick form creation with several nice data tracking features. There is also the option to reroute the form submission to a custom script. This is a very nice feature that makes it easy to control what happens after form submission. Here is a script I wrote that saves the data collected back to the wordpress database and emails the admin and form submitter.
Place this script somewhere in a folder under your Wordpress installation, and on the cforms admin page under Redirection, Messages, Text and Button Label set the Enable alternative form action! checkbox and then provide the URL to the script. Once a form is submitted you will still be able to track the submission via the admin tool.
<?php
//Make sure the user got here by way of form submission
// replace sendbutton2 with the name of your forms send button.
if (isset($_POST['sendbutton2'])) {
?>
<h3>Thanks for your submission.</h3>
<div>Some other detailed html message if you like…</div>
<?php
require(”wp-includes/class-phpmailer.php”);
////////////////////////////////
// Notification Email Recipients
$mailto = array();
//These addresses are for notification when the form is submitted.
$mailto[] = array(”address” => “someaddress1@somewhere.net”, “name” => “some person1″);
$mailto[] = array(”address” => “someaddress2@somewhere.net”, “name” => “some person2″);
$mailto[] = array(”address” => “someaddress3@somewhere.net”, “name” => “some person3″);
$mailfromaddress = ‘from@address.net’;
$mailfromname = ‘from name’;
$mailnotificationsubject = ‘Notification Subject’;
$mailconfirmationsubject = ‘Confirmation Subject’;
//////////////////////////////////////////////////////////////////
//Set the variables
//These come directly from cforms. You can use firebug to so see the field names, or just use the
//given form number. Example, the second field in form 2 will have ID cf2_field_2
// Of course you should customize this to your specific form.
$first = $_POST['cf2_field_2'];
$last = $_POST['cf2_field_3'];
$email = $_POST['cf2_field_4'];
$address = $_POST['cf2_field_5'];
$city = $_POST['cf2_field_6'];
$state = $_POST['cf2_field_7'];
$zip = $_POST['cf2_field_9'];
//etc etc etc
$link = mysql_connect(’DB-name usually localhost’, ‘DB-user’, ‘DB-password’);
if (!$link) {
die(’Could not connect: ‘ . mysql_error());
}
//Your wordpress schema name
mysql_select_db(”DB-schema-name”, $link);
mysql_query(”INSERT INTO wp_cformssubmissions (form_id, sub_date, email, ip) VALUES (’$formID’, NOW(), ‘$email’, ‘$_SERVER[REMOTE_ADDR]‘)”);
$id = mysql_insert_id();
//Insert the information block.
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘First Name’, ‘$first’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Last Name’, ‘$last’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Email’, ‘$email’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Address’, ‘$address’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘City’, ‘$city’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘State’, ‘$state’)”);
mysql_query(”INSERT INTO wp_cformsdata (sub_id, field_name, field_val) VALUES (’$id’, ‘Zip Code’, ‘$zip’)”);
mysql_close($link);
//Mail to all persons who should be notified.
$mail = new PHPMailer();
foreach($mailto as $to) {
$mail->AddAddress($to["address"], $to["name"]);
}
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = “smtp.someserver.net”; // Your SMTP server here
$mail->From = $mailfromaddress;
$mail->FromName = $mailfromname;
$mail->Subject = $mailnotificationsubject;
$mail->Body = <<<EOT
Hello Admin,<br/><br/>A new form submission has been completed …. <br/><br/>
<b>Details:</b><br/>
$first $last <br/>
$email <br/>
Login to http://yourwordpressinstance.org to review further registration information.
EOT;
$mail->AltBody = “<<<EOT
Hello Admin,
A new form submission has been completed ….
Details
$first $last
$email
$orderNum
Login to http://yourwordpressinstance.org to review further registration information.
EOT;”;
$mail->Send();
//Mail to form submitter
$mail = new PHPMailer();
$mail->AddAddress($email, $first.’ ‘.$last);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = “smtp.someserver.net”; // Your SMTP server here
$mail->From = $mailfromaddress;
$mail->FromName = $mailfromname;
$mail->Subject = $mailconfirmationsubject;
$mail->Body = <<<EOT
Hello $first,<br/><br/>Message to submitter<br/><br/>
<b>Details:</b><br/>
$first $last <br/>
$email <br/>
EOT;
$mail->AltBody = <<<EOT
Hello $first,
Message to submitter
Details:
$first $last
$email
EOT;
$mail->Send();
}
//Did not get here via form post, output an error.
else
echo “PAGE ERROR: You have tried to access this page from invalid context…”;
?>
