cfdp Posted May 27, 2013 Share Posted May 27, 2013 Hey guys. New to the forum here. I have created some custom content in the code, but most of it came from online resources. I would basically like to have an email go to a specific person when a customer fills out an inquiry form on the website.. HTML: <form name="contactform" method="post" action="sales_form.php"> <table width="541" border="0"> <tr> <td width="186" valign="top"> <label for="your_name">Your Name<span class="readmore"> *</span></label></td> <td width="345" valign="top"> <input type="text" name="your_name" maxlength="50" size="30" /> </td> </tr> <tr> <td valign="top"> <label for="practice_name">Practice Name<span class="readmore"> *</span></label></td> <td valign="top"> <input type="text" name="practice_name" maxlength="50" size="30" /> </td> </tr> <tr> <td valign="top"> <label for="practice_type">Practice Type<span class="readmore"> *</span></label></td> <td valign="top"><label for="practice_type"></label> <select id="practice_type" name="practice_type"> <option id="chiro" value="chiro">Chiropractic</option> <option id="pod" value="pod">Podiatry</option> <option id="ortho" value="ortho">Orthopaedic</option> <option id="vet" value="vet">Veterinary</option> <option id="other" value="other">Other</option> </select></td> </tr> <tr> <td valign="top"> <label for="email">Email<span class="readmore"> *</span></label></td> <td valign="top"> <input type="text" name="email" maxlength="80" size="30" /> </td> </tr> <tr> <td valign="top"> <label for="telephone">Phone<span class="readmore"> *</span></label></td> <td valign="top"> <input type="text" name="telephone" maxlength="30" size="30" /> </td> </tr> <tr> <td valign="top"> <label for="location">Location<span class="readmore"> *</span></label></td> <td valign="top"><select name="state" size="1"> <option selected value="">State...</option> <option value="None">None</option> <option value="">-- UNITED STATES --</option> <option value="Alabama">Alabama</option> <option value="Alaska">Alaska</option> <option value="Arizona">Arizona</option> <option value="Arkansas">Arkansas</option> <option value="California">California</option> <option value="Colorado">Colorado</option> <option value="Connecticut">Connecticut</option> <option value="Delaware">Delaware</option> <option value="Florida">Florida</option> <option value="Georgia">Georgia</option> <option value="Hawaii">Hawaii</option> <option value="Idaho">Idaho</option> <option value="Illinois">Illinois</option> <option value="Indiana">Indiana</option> <option value="Iowa">Iowa</option> <option value="Kansas">Kansas</option> <option value="Kentucky">Kentucky</option> <option value="Louisiana">Louisiana</option> <option value="Maine">Maine</option> <option value="Maryland">Maryland</option> <option value="Massachusetts">Massachusetts</option> <option value="Michigan">Michigan</option> <option value="Minnesota">Minnesota</option> <option value="Mississippi">Mississippi</option> <option value="Missouri">Missouri</option> <option value="Montana">Montana</option> <option value="Nebraska">Nebraska</option> <option value="Nevada">Nevada</option> <option value="New Hampshire">New Hampshire</option> <option value="New Jersey">New Jersey</option> <option value="New Mexico">New Mexico</option> <option value="New York">New York</option> <option value="North Carolina">North Carolina</option> <option value="North Dakota">North Dakota</option> <option value="Ohio">Ohio</option> <option value="Oklahoma">Oklahoma</option> <option value="Oregon">Oregon</option> <option value="Pennsylvania">Pennsylvania</option> <option value="Rhode Island">Rhode Island</option> <option value="South Carolina">South Carolina</option> <option value="South Dakota">South Dakota</option> <option value="Tennessee">Tennessee</option> <option value="Texas">Texas</option> <option value="Utah">Utah</option> <option value="Vermont">Vermont</option> <option value="Virginia">Virginia</option> <option value="Washington">Washington</option> <option value="West Virginia">West Virginia</option> <option value="Wisconsin">Wisconsin</option> <option value="Wyoming">Wyoming</option> <option value="">-- CANADA --</option> <option value="Alberta">Alberta</option> <option value="British Columbia">British Columbia</option> <option value="Manitoba">Manitoba</option> <option value="New Brunswick">New Brunswick</option> <option value="Newfoundland and Labrador">Newfoundland and Labrador</option> <option value="Northwest Territories">Northwest Territories</option> <option value="Nova Scotia">Nova Scotia</option> <option value="Nunavut">Nunavut</option> <option value="Ontario">Ontario</option> <option value="Prince Edward Island">Prince Edward Island</option> <option value="Quebec">Quebec</option> <option value="Saskatchewan">Saskatchewan</option> <option value="Yukon Territory">Yukon Territory</option> <option value="">-- OTHER --</option> <option value="Other location">Other location</option> </select></td> </tr> <tr> <td valign="top"> <label for="details">Additional Details</label></td> <td valign="top"> <textarea name="details" cols="46" rows="4"></textarea> </td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="Submit" /></td> </tr> </table> </form> PHP: <?php if(isset($_POST['email'])) { // EDIT THE 2 LINES BELOW AS REQUIRED $email_to = $practice_type; $email_subject = "**Sales Inquiry from Website**"; function died($error) { // your error code can go here echo "We are very sorry, but there were error(s) found with the form you submitted. "; echo "These errors appear below.<br /><br />"; echo $error."<br /><br />"; echo "Please go back and fix these errors.<br /><br />"; die(); } // validation expected data exists if(!isset($_POST['your_name']) || !isset($_POST['practice_name']) || !isset($_POST['practice_type']) || !isset($_POST['email']) || !isset($_POST['telephone']) || !isset($_POST['location'])) { died('We are sorry, but there appears to be a problem with the form you submitted.'); } $your_name = $_POST['your_name']; // required $practice_name = $_POST['practice_name']; // required $practice_type = $_POST['practice_type']; // required $email_from = $_POST['email']; // required $telephone = $_POST['telephone']; // required $location = $_POST['location']; // required $details = $_POST['details']; // not required $error_message = ""; $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; if(!preg_match($email_exp,$email_from)) { $error_message .= 'The email address you entered does not appear to be valid.<br />'; } if(strlen($your_name) < 2) { $error_message .= 'Your name does not appear to be valid.<br />'; } if(strlen($telephone) < 2) { $error_message .= 'The telephone number you entered do not appear to be valid.<br />'; } if(strlen($error_message) > 0) { died($error_message); } $email_message = "Form details below.\n\n"; function clean_string($string) { $bad = array("content-type","bcc:","to:","cc:","href"); return str_replace($bad,"",$string); } $email_message .= "Customer Name: ".clean_string($your_name)."\n"; $email_message .= "Practice Name: ".clean_string($practice_name)."\n"; $email_message .= "Practice Type: ".clean_string($practice_type)."\n"; $email_message .= "Email: ".clean_string($email_from)."\n"; $email_message .= "Telephone: ".clean_string($telephone)."\n"; $email_message .= "Location: ".clean_string($location)."\n"; $email_message .= "Addl Details: ".clean_string($details)."\n"; // This switch statement will dictate which email address we will send to and assign it to the $sendTo variable switch ($practice_type) { case "chiro": $practice_type = "[email protected]"; break; case "pod": $practice_type = "[email protected]"; break; case "ortho": $practice_type = "[email protected]"; break; case "vet": $practice_type = "[email protected]"; break; case "other": $practice_type = "[email protected]"; break; default: $practice_type = "[email protected]"; } // create email headers $headers = 'From: '.$email_from."\r\n". 'Reply-To: '.$email_from."\r\n" . 'X-Mailer: PHP/' . phpversion(); @mail($email_to, $email_subject, $email_message, $headers); } ?> <!-- include your own success html here --> Thank you for your submission. <br /> You will quickly hear from a Sales Professional! </p> <p>Redirecting to home page in 15 seconds.. <?php ?> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/ Share on other sites More sharing options...
rwhite35 Posted May 27, 2013 Share Posted May 27, 2013 (edited) Try setting up your headers this way: $headers['From'] = '[email protected]'; $headers['Reply-To'] = '[email protected]'; $headers['Subject'] = $_POST['subject']; Also I would consider using a mail framework like PEAR Mail. http://pear.php.net/package/Mail/redirectedHere is the PEAR Mail implementation: /* -- PEAR::MAIL * PEAR::Mail only opens one mail socket */ require_once('/local/path/to/php/Mail.php'); //this would be the path to your PEAR Mail library $recipients = [string] or [array]; $headers['From'] = '[email protected]'; $headers['Reply-To'] = '[email protected]'; $headers['Subject'] = $_POST['subject']; $params['sendmail_path'] = '/usr/sbin/sendmail'; //using sendmail on backend $mail_object =& Mail::factory('sendmail',$params); //using factory method Then you call the class and send the mail //send the mail instance $mail_object->send($recipients,$headers,$email_message); if (PEAR::isError($mail_object)) {print($mail_object->getMessage());} FInally, I would move the form validation to the client side using JQuery and Javascript. You would still scrub the input like you have it or you could also use one of PHP's build in function. Edited May 27, 2013 by rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/#findComment-1432590 Share on other sites More sharing options...
rwhite35 Posted May 27, 2013 Share Posted May 27, 2013 Here is a JQuery/Javascript function I like to use: <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function() { //client side field validation before submit processing var regexp = /^([A-Za-z0-9_\+\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; $("form").submit(function() { if ($("input:first").val()==''){ alert("You need a subject!"); return false; } else if ($('input[name="uname"]').val()==''){ alert ("You need a name!"); return false; } else if ($('input[name="uemail"]').val()=='' || ($('input[name="uemail"]').val().match(regexp)<1)){ alert ("You need a value email address"); return false; } else { //if valid, send data for server side processing //$.ajax({url:'lib/emailProcess.php',type:'post'}); alert("Your email was submitted"); return true; }; }); }); </script> Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/#findComment-1432593 Share on other sites More sharing options...
DavidAM Posted May 27, 2013 Share Posted May 27, 2013 FInally, I would move the form validation to the client side using JQuery and Javascript. You would still scrub the input like you have it or you could also use one of PHP's build in function. You can ADD validation on the client, but you should NOT move the validation there. The client can disable JavaScript and post anything they want, so you absolutely need to do validation on the server. Doing validation on the client is only for the user's benefit (immediate feedback and fewer round-trips to the server, so faster). @OP Are you having problems with the posted code? Tell us what it does that it should not do; or what is does not do that it should. Turn on error reporting and remove the error suppressor (@) from your code. The "@" only hides an error, it does not magically fix it. You need to see the error message so you can fix your code. // Turn on error reporting - Near the top of the script error_reporting(-1); ini_set('display.errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/#findComment-1432595 Share on other sites More sharing options...
cfdp Posted May 27, 2013 Author Share Posted May 27, 2013 (edited) Thanks so much for the feedback! I basically want to have a form that, when filled out by the user, gets submitted to the corresponding email, depending on what 'practice_type' is selected. After enabling error reporting, I get this error message: Notice: Undefined variable: practice_type in /home/content/81/8359381/html/site/contact/sales_form.php on line 64 Which yields to this line: $email_to = $practice_type; I wasn't sure if it would work, now I know it doesn't! What is the advantage of using Perl? Edited May 27, 2013 by cfdp Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/#findComment-1432598 Share on other sites More sharing options...
rwhite35 Posted May 27, 2013 Share Posted May 27, 2013 Regarding instantiating your $_POST variables, you might consider this technique. // convert $_POST array into scalar variables // example $_POST['practice_type'] = somestring // becomes $practice_type = somestring if(!empty($_POST)) { foreach($_POST as $key=>$value) { ${$key} = $value; } } // now process your variables a scalar vars You would use this in your first few line of code after you scrubbed the $_POST array. Regarding PEAR Mail, it optimizes your email and allows you to send multiple emails without having to call the mail function multiple times. So its safer and faster then mail function. PEAR Mail is part of the PHP distro since 5, if you can cd to your PHP binary, you should see a folder called Mail and a file called Mail.php. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/#findComment-1432621 Share on other sites More sharing options...
DavidAM Posted May 28, 2013 Share Posted May 28, 2013 Blindly creating variables from the $_POST (or $_GET) array is NOT a safe practice. I could add a field to the form with a name of "IsAdmin" and a value of "1" (or true); and post the form. If your script uses a variable named $IsAdmin the POST value could overwrite it giving me admin priviledges. This is the reason that "register globals" has been removed from PHP. It is better and safer to assign specifically those POST values that you are expecting. @OP That line of code is referencing $practice_type which you do not assign until somewhere around line 83. The example you copied it from probably depended on register_globals being on. It isn't going to work that way anyway, since you don't assign TO email address to practice_type until the switch statement around line 119. Move the offending line below that switch (or remove it and have the switch assign the address directly to $email_to). Quote Link to comment https://forums.phpfreaks.com/topic/278443-cannot-get-basic-php-form-to-work/#findComment-1432779 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.