nwhysel Posted June 30, 2010 Share Posted June 30, 2010 I can't seem to get this script to both email the form results and save to a mysql database. Right now it saves to the database, but refuses to send the email. The part of the script in question is after the validation section just after the line case "no": which indicates that there are no validation errors, so go ahead and send the data. Script copied below and also attached along with an included file that connects to the database. Can someone help? <? /* I. SET forms action: add or validate. A. IF add, set values as empty and show blank form. B. IF edit, show populated form. C. IF validate, format and validate 1. IF valid, send data to Alaric. 2. IF not valid, set error messages and redisplay populated form. */ /* 1. SET form action: add or validate. If the form has been submitted, then the action is "validate". If the form hasn't been submitted, then the action is "add". If the form has been submitted, and we have no errors, then the action is "send". --------------------------------------- */ if ($_POST[submitEmail]) { $action_type = "validate";} else { /* otherwise we're adding a new one */ $action_type = "add"; } /* END if $_POST[submitEmail] */ /* SWITCH form display based on add, edit or validate */ switch ($action_type) { /* ADD: SET values for the form */ case "add": /* SET empty values for form inputs */ $opt_in = "Yes"; $please_contact_me = "Yes"; $FirstName = "" ; $LastName = "" ; $Title = ""; $OrgName= ""; $Email = ""; $Phone = ""; $Address1 = ""; $Address2= ""; $City= ""; $StateProvince = ""; $PostalCode = ""; $Country = ""; $comments= ""; $DatePosted = date("U"); /* SET CSS classes for form inputs */ $opt_in_class = ""; $please_contact_me_class = ""; $FirstName_class = "required"; $LastName_class = "required"; $Title_class = ""; $OrgName_class = "required"; $Email_class = "required"; $Phone_class = ""; $Address1_class = ""; $Address2_class = ""; $City_class = ""; $StateProvince_class = ""; $PostalCode_class = ""; $Country_class = ""; $comments_class = ""; break; /* END case = "add" */ /* VALIDATE: check for data in required fields. */ case "validate": /* SET error status to "no" before we begin validation. */ $data_error = "no"; /* VALIDATE FirstName: required */ if (! isset($_POST[FirstName]) || $_POST[FirstName] == "" ) { $data_error = "yes"; $FirstName_class = "error"; $FirstName_error = "Please enter your First Name."; } /* VALIDATE LastName: required */ if (! isset($_POST[LastName]) || $_POST[LastName] == "" ) { $data_error = "yes"; $LastName_class = "error"; $LastName_error = "Please enter your Last Name."; } /* VALIDATE OrgName: required */ if (! isset($_POST[OrgName]) || $_POST[OrgName] == "" ) { $data_error = "yes"; $OrgName_class = "error"; $OrgName_error = "Please enter your Organization Name."; } /* VALIDATE Email: required */ if (! isset($_POST) || $_POST == "" ) { $data_error = "yes"; $Email_class = "error"; $Email_error = "Please enter your Email Address."; } // validate $Phone $notel = array("+", " ", "(", ")", "[", "]", "-", ",", "#") ; $tel = str_replace ($notel, array("00"), $tel) ; /* FORMAT and VALIDATE input data. TEMPORARILY: we have no validation. We'll add it later... */ $opt_in = $_POST[opt_in]; $please_contact_me = $_POST[please_contact_me]; $FirstName = $_POST[FirstName]; $LastName = $_POST[LastName]; $Title = $_POST[Title]; $OrgName = $_POST[OrgName]; $Email = $_POST; $Phone = $_POST[Phone]; $Address1 = $_POST[Address1]; $Address2 = $_POST[Address2]; $City = $_POST[City]; $StateProvince = $_POST[stateProvince]; $PostalCode = $_POST[PostalCode]; $Country = $_POST[Country]; $comments = $_POST[comments]; $DatePosted = date("U", strtotime($_POST[DatePosted])); /* SWITCH action based on error (yes or no) */ switch ($data_error) { /* NO ERROR. input form data */ case "no": /* MAIL administrator that there is an inquiry waiting for them */ $message = "Opt In? " . $opt_in . "\n"; $message = $message ."Contact Me? " . $please_contact_me . "\n\n"; $message = $message ."First Name: " . $FirstName . "\n"; $message = $message ."Last Name: " . $LastName . "\n"; $message = $message ."Title: " . $Title . "\n"; $message = $message ."Organization: " . $OrgName . "\n"; $message = $message ."Email: " . $Email . "\n"; $message = $message ."Phone: " . $Phone . "\n"; $message = $message ."Address1: " . $Address1 . "\n"; $message = $message ."Address2: " . $Address2 . "\n"; $message = $message ."City: " . $City . "\n"; $message = $message ."State/Province: " . $StateProvince . "\n"; $message = $message ."Postal Code: " . $PostalCode . "\n"; $message = $message ."Country: " . $Country . "\n\n"; $message = $message ."Comments: " . "\n\n" . $comments ; $from = $Email; $headers = "From: $from"; mail("[email protected]", "Website Inquiry", $message, $headers); /* END MAIL */ // INSERT new Inquiry to mysql database // 1. Open db connection // 2. Build insert query // 3. INSERT new inquiry into table // 4. CLOSE database // 1. OPEN database connection (include object: connect-to-mysql.php) include($_SERVER["DOCUMENT_ROOT"]."/includes/connect-to-mysql.php"); // 2. Build insert query $insert = " INSERT INTO inquiries (opt_in, please_contact_me, FirstName, LastName, Title, OrgName, Email, Phone, Address1, Address2, City, StateProvince, PostalCode, Country, comments) VALUES ('$opt_in','$please_contact_me','$FirstName','$LastName','$Title','$OrgName','$Email','$Phone','$Address1','$Address2','$City','$StateProvince','$PostalCode','$Country','$comments')"; // 3. INSERT new web inquiry mysqli_query($connection, $insert); // 4. CLOSE database mysqli_close($connection); /* REDIRECT to Thank You page */ /* header("Location: thank-you-test-contact.php"); */ header("Location: thank-you-test-contact.php"); exit; break; /* END data_error ="no" */ case "yes": /* TEMPORARY: If we have an error this is where we'd change the title to reflect and error, set the global error message, and then redisplay the form. */ $page_title = "Oops! There was an error with your form submission"; $submit_text = "Re-submit inquiry"; break; /* END data_error ="yes" */ } /* END switch on error (yes or no) */ break; /* END case ="validate" */ } /* END switch on $action_type: add, edit, or validate */ ?> <?php $page_title = "Contact Us"; $page_subtitle = "Alaric Compliance Services"; $sectionDir = "home"; $section = "Contact Us"; $pageName = ""; ?> <head> <title><?php echo $page_title; ?>: <?php echo $page_subtitle; ?></title> <META NAME="resource-type" CONTENT="document"> <META HTTP-EQUIV="pragma" CONTENT="no-cache"> <Meta http-equiv="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <META NAME="revisit-after" CONTENT="25"> <META NAME="classification" CONTENT=""> <meta name="keywords" content="<?php require_once($_SERVER["DOCUMENT_ROOT"].'/includes/modules/keywords.php')?>, Alaric"> <meta name="description" content=""> <META NAME="robots" CONTENT="ALL"> <META NAME="distribution" CONTENT="Global"> <META NAME="rating" CONTENT="General"> <META NAME="copyright" CONTENT=""> <META NAME="author" CONTENT=""> <META HTTP-EQUIV="reply-to" CONTENT=""> <META NAME="language" CONTENT="English"> <META NAME="doc-type" CONTENT="Web Page"> <META NAME="doc-class" CONTENT="Completed"> <META NAME="doc-rights" CONTENT="Public"> <link rel="stylesheet" type="text/css" href="http://alariccompliance.com/alaric-styles.css" /> </head> <?php /* Display error message if we have errors */ if ($data_error == "yes") { echo "<p style='color: red;'>We apologize, but there is an error in your submission. Please correct the problems highlighted below and resend.</p>"; } ?> <!-- Show the default suggestion form if the user hasn't submitted it yet --> <form action="<? echo $_SERVER['PHP_SELF'] ?>" method="post"> <h4>Sign up for Alaric Updates and Events</h4> <?php // print error message if any if ( isset($opt_in_error)){ echo '<span style="color:red;">'.$opt_in_error.'</span><br />'; }?> <p><Input type = 'checkbox' Name ='opt_in' value= '<?php echo $opt_in; ?>'> I would like to receive regular compliance updates from Alaric and invitations to special client events and conferences.</p> <h4>Contact an Alaric Team Member</h4> <?php // print error message if any if ( isset($please_contact_me_error)){ echo '<span style="color:red;">'.$please_contact_me_error.'</span><br />'; }?> <p><Input type = 'checkbox' Name ='please_contact_me' value= '<?php echo $please_contact_me; ?>'> I would like to be contacted by an Alaric team member.</p> <p>Unless you opt to receive our newsletter and updates, your email address will only be used for response purposes.<br /><br /> <table border="0"><tr> <td valign="top"> <?php // print error message if any if ( isset($FirstName_error)){ echo '<span style="color:red;">'.$FirstName_error.'</span><br />'; }?> First Name <span style="background: #ddeeff;">(Required)</span><br /> <INPUT name="FirstName" type=text size=32 maxlength=64 value="<?php echo $FirstName; ?>"><br /> <?php // print error message if any if ( isset($LastName_error)){ echo '<span style="color:red;">'.$LastName_error.'</span><br />'; }?> Last Name <span style="background: #ddeeff;">(Required)</span><br /> <INPUT name="LastName" type=text size=32 maxlength=64 value="<?php echo $LastName; ?>"> <br /> Title <br /> <INPUT name="Title" type=text size=32 maxlength=64 value="<?php echo $Title; ?>"> <br /> <?php // print error message if any if ( isset($OrgName_error)){ echo '<span style="color:red;">'.$OrgName_error.'</span><br />'; }?> Organization Name <span style="background: #ddeeff;">(Required)</span><br /> <INPUT name="OrgName" type=text size=32 maxlength=64 value="<?php echo $OrgName; ?>"> <br /> <?php // print error message if any if ( isset($Email_error)){ echo '<span style="color:red;">'.$Email_error.'</span><br />'; }?> Email Address <span style="background: #ddeeff;">(Required)</span><br /> <INPUT name="Email" type=text size=32 maxlength=64 value="<?php echo $Email; ?>"> <small>(<i>[email protected]</i>)</small><br /> Phone <br /> <INPUT name="Phone" type=text size=32 maxlength=64 value="<?php echo $Phone; ?>"> <small>(<i>xxx-xxx-xxxx</i>) </small><br /> </td> <td> Street Address Line 1 <br /> <INPUT name="Address1" type=text size=32 maxlength=64 value="<?php echo $Address1; ?>"><br /> Street Address Line 2 <br /> <INPUT name="Address2" type=text size=32 maxlength=64 value="<?php echo $Address2; ?>"><br /> City <br /> <INPUT name="City" type=text size=32 maxlength=64 value="<?php echo $City; ?>"><br /> State/Province <br /> <INPUT name="StateProvince" type=text size=32 maxlength=64 value="<?php echo $StateProvince; ?>"><br /> Postal Code <br /> <INPUT name="PostalCode" type=text size=32 maxlength=64 value="<?php echo $Country; ?>"><br /> Country <br /> <INPUT name="Country" type=text size=32 maxlength=64 value="<?php echo $comments; ?>"><br /> </td> </tr> </table> Comments: <br /> <textarea type="text" name="comments" COLS="40" ROWS="4"></textarea> <br /> <input name="submitEmail" type="Submit" value="Submit"></p> </form> </body> </html> [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/206337-attempting-to-email-results-of-webform-and-save-to-mysql/ Share on other sites More sharing options...
nwhysel Posted June 30, 2010 Author Share Posted June 30, 2010 Wouldn't you know it, when I went back to the last email that actually did work, I noticed that one field was referencing another field's content. I reviewed the form itself and realized that a line was cut off causing the erorr that ultimately caused it to not send email. Phew! Link to comment https://forums.phpfreaks.com/topic/206337-attempting-to-email-results-of-webform-and-save-to-mysql/#findComment-1079400 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.