cforms ii custom submit script

Friday, August 15th, 2008 @ 8:32 am | 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…”;
?>

 

Recently

  • I will never buy an Adobe product ever again!
  • XNA Tutorial: HLSL and SpriteBatch for 2D effects.
  • Will Philly make me cry again this week
  • Photoshop takes a long time to open or create files
  • Fight Monkey of Magic
  • David Stern must read my blog
  • The most over hyped non-storm of all time
  • cforms ii custom submit script
  • wordpress get_parent function
  • Flexible Navigation – Wordpress Plugin *beta
  •  

    7 Responses to “cforms ii custom submit script”

    1. Victor Says:

      Hi,

      Do you know if this script still works with the latest CformsII? I have been trying for days to do something that seems fairly simple, but haven’t found the solution ANYWHERE! I am using an autoresponder and wanted to use CformsII in my wordpress blog and have the variables “POST” to the url of the send page so that I could make a custom Thank You page with the persons information on it. Such as, “Hello Dan, thank you for submitting your question.” While at the same time I want the variables to automatically be sent to my aweber autoresponder account.

      Any ideas on this? I couldn’t get your script to work, but wondered if it’s because it’s been a year or more since it was written.

      Thank you so much in advance!

    2. Jesse Says:

      The script itself is nothing but a basic mailer, and the interfacing with Cforms is quite simple (and would be easy to fix if something changed). Could you provide details, such as error messages or what is not working, It could be that your smtp settings are not correct or that sending email is blocked by you hosting service.

    3. Victor Says:

      Hi Jesse…thx for responding,

      I might not have been clear with what I want to do. In Cforms, there is a way you can “redirect” to another page once someone presses the send button on the form. What I am trying to do is send the form fields, in other words the person’s name and email to my aweber autoresponder service in the background and then forward them to a “Thank You for signing up” page within my wordpress blog. I am trying to get Cforms to send (Post/Get) the field values to my autoresponder which then forwards those variables back to my blog within the url (i.e. http://blogname.com/thankyou/thankyou.php?name=Joe&from=joe@joe.com). I can do this with a regular html send form within wordpress, even though a second issue I ran into now is that wordpress will reject the “name=” value and results in a 404 error.

      I don’t know if I am making sense here, but I not getting errors in Cforms, I am not understand how to get Cforms to send POST variables to a url. I believe it is somewhere in the functions.php file, but it is confusing me. Sorry for the long msg and thanks!

    4. Jesse Says:

      Ok, I see. This should be doable. If I were you, I would use firebug to see what the post parameter are. If you are not familiar, the firebug console looks like this.

      http://vaadin.com/download/current/docs/book/img/debug/firebug-net-uidl.png

      You can view tabs for the post/get. You should then be able to grab those post parameters on the page that is posted to, send the email and present whatever message you want. Let me know if I can help further.

    5. matt Says:

      Victor, did you ever get this to work with aweber? I would like to hear the results of it as well as how you did it…

      Not being a programmer, a step by step tutorial on how to accomplish this would be sweet! I’m sure it would bring a bunch of site visitors.

      It would also make sense for cforms to add this functionality into their plugin.

    6. Jewell Kenefick Says:

      what a great site and informative posts, I will bookmark your site. Keep up the good work!

    7. glamourwatcher Says:

      Hi,

      Could someone please mail me a simple tutorial of how I can redirect the data from the Cform wp comment form into a profile page. With possible storageof this data in the database. I am a wordpress php geek. The cform plugin is installed, activated. But the help and api info ?? Please a basic steps guide ? Many thanks!
      glamourwatcher@hotmail.com

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">