ugreen Posted December 6, 2012 Share Posted December 6, 2012 Hi all, I have a contact form that works fine, I just need some REALLY BASIC validation added. As long as someone types in ANYTHING into the Name and Email fields to send the form. I would really appreciate some really simple validation that would work with what I have. I honestly have no idea how I got this far. THANKS IN ADVANCE! Here is the form code: -------------- <form method="post" action="sendmailscript.php"> <fieldset> <legend>Contact Information:</legend> <div> <label>Name*</label> <input name="name" type="text" required="required"/> <br /> </div> <div> <label>Company Name</label> <input name="company" type="text"/> <br /> </div> <div> <label>Phone Number</label> <input name="phone" type="tel" /> <br /> </div> <div> <label>Email Address*</label> <input name="email" type="email" required="required" /> <br /> </div> <div> <label>Website Address</label> <input name="website" type="url" /> <br /> </div> </fieldset> <fieldset> <legend>Preferred Meeting Type:</legend> <div> <label> </label> <input type="radio" name="meeting" value="Go To Meeting" /> <span class="checkboxDesc">Set Up A Go To Meeting Online Demo</span><br /> <label> </label> <input type="radio" name="meeting" value="Face To Face" /> <span class="checkboxDesc">Contact Me About A Face-To-Face Meeting</span><br /> <label> </label> <input type="radio" name="meeting" value="Would Like More Information" /> <span class="checkboxDesc">More Information</span><br /> </div> </fieldset> <fieldset> <legend>I'm Interested In:</legend> <div> <label>Request Information On:</label> <input type="checkbox" name="services[]" value="Call Tracking Telephone Numbers" /> <span class="checkboxDesc">Call Tracking Telephone Numbers</span><br /> <input type="checkbox" name="services[]" value="Design, Print & Mail Direct Mail Packages" /> <span class="checkboxDesc">Design, Print & Mail Packages</span><br /> <input type="checkbox" name="services[]" value="Direct Mail List Purchase" /> <span class="checkboxDesc">Direct Mail List Purchase</span><br /> <input type="checkbox" name="services[]" value="Printing & Graphic Design Services" /> <span class="checkboxDesc">Printing & Graphic Design Services</span><br /> <input type="checkbox" name="services[]" value="Website Design Services" /> <span class="checkboxDesc">Website Design Services</span><br /> <input type="checkbox" name="services[]" value="QR Codes & Landing Pages" /> <span class="checkboxDesc">QR Codes & Landing Pages</span><br /> <input type="checkbox" name="services[]" value="Mobile Websites" /> <span class="checkboxDesc">Mobile Websites</span><br /> <input type="checkbox" name="services[]" value="Sales & Lead Tracking Software" /> <span class="checkboxDesc">Sales & Lead Tracking Software</span><br /> <input type="checkbox" name="services[]" value="Marketing & Sales Consulting Services" /> <span class="checkboxDesc">Marketing & Sales Consulting Services</span><br /> </div> <div> <label>Message</label> <textarea name="message" id="message" rows="5" required="required" /> </textarea> </div> </fieldset> <input type="submit" name="submit" value="Submit" class="button" /> </form> ______________ Here is the php code: ----------- <?php $EmailTo = "me@me.com"; $Subject = "Request Form"; $Name = Trim(stripslashes($_POST['name'])); $Company = Trim(stripslashes($_POST['company'])); $Tel = Trim(stripslashes($_POST['phone'])); $Email = Trim(stripslashes($_POST['email'])); $Website = Trim(stripslashes($_POST['website'])); $Meeting = $_POST['meeting']; $Services = Implode("\n", $_POST['services']); $Message = Trim(stripslashes($_POST['message'])); $mailheader .= "Reply-To: $Email \r\n"; // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Phone: "; $Body .= $Tel; $Body .= "\n"; $Body .= "Email: "; $Body .= "$Email"; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n\n"; $Body .= "Preferred Meeting Type: \n"; $Body .= $Meeting; $Body .= "\n\n"; $Body .= "I'm Interested In: \n"; $Body .= $Services; $Body .= "\n\n"; $Body .= "Message: \n"; $Body .= $Message; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$Email>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/ Share on other sites More sharing options...
lovephp Posted December 6, 2012 Share Posted December 6, 2012 if(empty($_POST['email']) || empty($_POST['name'])){ echo 'fields are required'; } ////submit the form something like this? Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397906 Share on other sites More sharing options...
ugreen Posted December 6, 2012 Author Share Posted December 6, 2012 Yes. That's really all I need. Sorry, now where do I incorporate this? Also, can I choose where the error will be displayed? Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397916 Share on other sites More sharing options...
ugreen Posted December 6, 2012 Author Share Posted December 6, 2012 What happens is, the message gets displayed in a new window and the form gets sent anyway. I guess I just want to validate that there is something in the name and emails fields and email only be sent IF there is something in those fields. Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397918 Share on other sites More sharing options...
lovephp Posted December 6, 2012 Share Posted December 6, 2012 (edited) try this and see <?php if(isset($_POST['submit'])){ if(empty($_POST['name']) || empty($_POST['email'])){ echo 'Name and E-mail is required'; }else{ $EmailTo = "me@me.com"; $Subject = "Request Form"; $Name = Trim(stripslashes($_POST['name'])); $Company = Trim(stripslashes($_POST['company'])); $Tel = Trim(stripslashes($_POST['phone'])); $Email = Trim(stripslashes($_POST['email'])); $Website = Trim(stripslashes($_POST['website'])); $Meeting = $_POST['meeting']; $Services = Implode("\n", $_POST['services']); $Message = Trim(stripslashes($_POST['message'])); $mailheader .= "Reply-To: $Email \r\n"; // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Phone: "; $Body .= $Tel; $Body .= "\n"; $Body .= "Email: "; $Body .= "$Email"; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n\n"; $Body .= "Preferred Meeting Type: \n"; $Body .= $Meeting; $Body .= "\n\n"; $Body .= "I'm Interested In: \n"; $Body .= $Services; $Body .= "\n\n"; $Body .= "Message: \n"; $Body .= $Message; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$Email>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } } } ?> Edited December 6, 2012 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397932 Share on other sites More sharing options...
ugreen Posted December 6, 2012 Author Share Posted December 6, 2012 That works great, except how do I display that error in the same page (maybe a separate span)? Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397935 Share on other sites More sharing options...
lovephp Posted December 6, 2012 Share Posted December 6, 2012 u could use array to display error msgs on the form google and see there are loads of examples Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397939 Share on other sites More sharing options...
ugreen Posted December 6, 2012 Author Share Posted December 6, 2012 Thanks for your help. I've been trying to use 'include' and other options, but none have worked so far. I might have to skip the validation. Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397953 Share on other sites More sharing options...
premiso Posted December 6, 2012 Share Posted December 6, 2012 u cud also tpy lik dis to mak urslf sem c00l! Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397966 Share on other sites More sharing options...
lovephp Posted December 6, 2012 Share Posted December 6, 2012 functions.php <?php function formError($p){ $errormsg = array(); if($p['name'] == '') { $errormsg['name'] = 'Name is required!'; } if($p['email'] == '') { $errormsg['email'] = 'E-mail is required!'; }else if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $p['email'])) { $errormsg['email'] = 'E-mail is not valid!'; } return $errormsg; } ?> sendmail.php <?php include ("functions.php"); if(count($_POST)){ $f = $_POST; $error = formError($f); if(count($error) == 0) { $EmailTo = "me@me.com"; $Subject = "Request Form"; $Name = Trim(stripslashes($_POST['name'])); $Company = Trim(stripslashes($_POST['company'])); $Tel = Trim(stripslashes($_POST['phone'])); $Email = Trim(stripslashes($_POST['email'])); $Website = Trim(stripslashes($_POST['website'])); $Meeting = $_POST['meeting']; $Services = Implode("\n", $_POST['services']); $Message = Trim(stripslashes($_POST['message'])); $mailheader .= "Reply-To: $Email \r\n"; // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Company: "; $Body .= $Company; $Body .= "\n"; $Body .= "Phone: "; $Body .= $Tel; $Body .= "\n"; $Body .= "Email: "; $Body .= "$Email"; $Body .= "\n"; $Body .= "Website: "; $Body .= $Website; $Body .= "\n\n"; $Body .= "Preferred Meeting Type: \n"; $Body .= $Meeting; $Body .= "\n\n"; $Body .= "I'm Interested In: \n"; $Body .= $Services; $Body .= "\n\n"; $Body .= "Message: \n"; $Body .= $Message; // send email $success = mail($EmailTo, $Subject, $Body, "From: <$Email>"); // redirect to success page if ($success){ print "<meta http-equiv=\"refresh\" content=\"0;URL=thanks.html\">"; } else{ print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">"; } } } ?> <form method="post" action="sendmail.php"> <fieldset> <legend>Contact Information:</legend> <div> <label>Name*</label> <input name="name" type="text" required="required"/> <?php if($error['name'] !='') { echo '<div class="error_msg">'.$error['name'].'</div>'; } ?> <br /> </div> <div> <label>Company Name</label> <input name="company" type="text"/> <br /> </div> <div> <label>Phone Number</label> <input name="phone" type="tel" /> <br /> </div> <div> <label>Email Address*</label> <input name="email" type="email" required="required" /> <?php if($error['email'] !='') { echo '<div class="error_msg">'.$error['email'].'</div>'; } ?> <br /> </div> <div> <label>Website Address</label> <input name="website" type="url" /> <br /> </div> </fieldset> <fieldset> <legend>Preferred Meeting Type:</legend> <div> <label> </label> <input type="radio" name="meeting" value="Go To Meeting" /> <span class="checkboxDesc">Set Up A Go To Meeting Online Demo</span><br /> <label> </label> <input type="radio" name="meeting" value="Face To Face" /> <span class="checkboxDesc">Contact Me About A Face-To-Face Meeting</span><br /> <label> </label> <input type="radio" name="meeting" value="Would Like More Information" /> <span class="checkboxDesc">More Information</span><br /> </div> </fieldset> <fieldset> <legend>I'm Interested In:</legend> <div> <label>Request Information On:</label> <input type="checkbox" name="services[]" value="Call Tracking Telephone Numbers" /> <span class="checkboxDesc">Call Tracking Telephone Numbers</span><br /> <input type="checkbox" name="services[]" value="Design, Print & Mail Direct Mail Packages" /> <span class="checkboxDesc">Design, Print & Mail Packages</span><br /> <input type="checkbox" name="services[]" value="Direct Mail List Purchase" /> <span class="checkboxDesc">Direct Mail List Purchase</span><br /> <input type="checkbox" name="services[]" value="Printing & Graphic Design Services" /> <span class="checkboxDesc">Printing & Graphic Design Services</span><br /> <input type="checkbox" name="services[]" value="Website Design Services" /> <span class="checkboxDesc">Website Design Services</span><br /> <input type="checkbox" name="services[]" value="QR Codes & Landing Pages" /> <span class="checkboxDesc">QR Codes & Landing Pages</span><br /> <input type="checkbox" name="services[]" value="Mobile Websites" /> <span class="checkboxDesc">Mobile Websites</span><br /> <input type="checkbox" name="services[]" value="Sales & Lead Tracking Software" /> <span class="checkboxDesc">Sales & Lead Tracking Software</span><br /> <input type="checkbox" name="services[]" value="Marketing & Sales Consulting Services" /> <span class="checkboxDesc">Marketing & Sales Consulting Services</span><br /> </div> <div> <label>Message</label> <textarea name="message" id="message" rows="5" required="required" /> </textarea> </div> </fieldset> <input type="submit" name="submit" value="Submit" class="button" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1397974 Share on other sites More sharing options...
Christian F. Posted December 6, 2012 Share Posted December 6, 2012 I would strongly recommend against using the script that lovephp posted! It is completely unsafe and allows any malicious user to easily take control of it, and use it for sending spam to the entire world. From your account. Instead I recommend you use something like this: <?php if (isset ($_POST['submit'])) { $error = $services = array (); $website = ''; // Define an array of the available meeting types. Make sure it starts at 1 for use in form-validation. $availableMeetings = array ( 1 => "Set Up A Go To Meeting Online Demo", "Contact Me About A Face-To-Face Meeting", "More Information", ); // Create the output template for the meetingsradio buttons. $meetingsTemplate = <<<OutHTML <input tid="inp_meeting_%1\$d" type="radio" name="meeting" value="%1\$" /> <label for="inp_meeting_%1\$d" class="checkboxDesc">%2\$s</label> OutHTML; // Define an array of available services. Make sure it starts at 1 for use in form-validation. $availableServices = array ( 1 => "Call Tracking Telephone Numbers", "Call Tracking Telephone Numbers", "Design, Print & Mail Packages", "Direct Mail List Purchase", "Printing & Graphic Design Services", "Website Design Services", "QR Codes & Landing Pages", "Mobile Websites", "Sales & Lead Tracking Software", "Marketing & Sales Consulting Services", ); // Define the template for the service checkboxes. $serviceTemplate = <<<OutHTML <label for="inp_service_%1\$d" class="checkboxDesc">%2\$s</label> <input id="inp_service_%1\d" type="checkbox" name="services[]" value="%1\$d" /> OutHTML; // Define the RegExp to validate names by, and make sure name is legit. $nameRegExp = '/^[a-zA-Z\\pL][\\w\\pL \\.\'\\-]{0,60}\\z/u'; if (empty ($_POST['name']) || !preg_match ($nameRegExp, $_POST['name'])) { $error[] = 'Missing or invalid name. '; } // Validate e-mail address, to prevent IMAP header injections. if (!$formData['email'] = filter_var ($_POST['email'], FILTER_VALIDATE_EMAIL)) { $error[] = "Invalid e-mail address."; } // If website is set, make sure it's a valid URL. if (!empty ($_POST['website']) && $formData['website'] = filter_var ($_POST['website'], FILTER_VALIDATE_URL)) { $error[] = "Invalid website-address."; } // If phone number was provided, make sure it's all numbers. if (!empty ($_POST['phone']) && !$formData['phone'] = filter_var ($_POST['phone'], FILTER_VALIDATE_INT)) { $error[] = "Invalid phone number."; } // Validate the preferred meeting type. if (!isset ($availableMeetings[$_POST['meeting']])) { $error[] = "Invalid meeting type."; } else { $meeting = $availableMeetings[$_POST['meeting']]; } // Loop through and validate all checked service IDs. foreach ($_POST['services'] as $serviceID) { if (!isset ($availableServices[$serviceID])) { // Not a valid service ID, add to errors and skip to next element. $error[] = "$ServiceID is an invalid service ID."; continue; } $services[] = $serviceID; } // Add the "too broad to validate properly" input fields to data array. $formData['name'] = trim ($_POST['name']); $formData['company'] = trim ($_POST['company']); $formData['message'] = trim ($_POST['message']); // If any errors have been found, format them properly, generate the form, and stop parsing. if (!empty ($error)) { $error = '<ul class="error"><li>'.implode ("</li>\n\t<li>", $error)."</li>\n</ul>"; echo Gen_Form ($error, $formData); return; } // Set some meta-information about the mails. $EmailTo = "me@me.com"; $Subject = "Request Form"; $mailheader .= "Reply-To: $Email\r\n"; // Prepare email body text $Body = <<<OutMail Name: {$formData['name']} Company: {$formData['company']} Phone: {$formData['tel']} Email: {$formData['email']} Website: {$formData['website']} Preferred Meeting Type: {$formData['meeting']} I'm Interested In: {$formData['services']} Message: {$formData['message']} OutMail; // send email $success = mail ($EmailTo, $Subject, $Body, "From: <$Email>"); // redirect to success page if ($success) { header ("Location: {$_SERVER['PHP_SELF']}send=ok'"); die (); } $error = '<h1 class="error">Error</h1>'."\n". '<p class="error">Mail was not send correctly, please try again or contant the '. 'administrator if the problem persists.</p>'; echo genForm ($error, $formData); return; } function genForm ($message = '', $formData = array (), $radioData, $checkboxData) { // TODO: Write the function that generates the readio, and the one for checkboxes. // Then display the completed form. Remember to use htmlspecialchars () // around text input from the user. } I've done most of it for you, but I've left the HTML generation up to you. Should be a nice exercise, and I'm just about ready to fall asleep where I'm sitting. If there's something you're wondering about, please do ask and I'm sure lots of people will help. Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1398000 Share on other sites More sharing options...
ugreen Posted December 7, 2012 Author Share Posted December 7, 2012 Thank you both! I will have to play around with both of these and see what I can get working! Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1398107 Share on other sites More sharing options...
splinter_1234 Posted February 9, 2013 Share Posted February 9, 2013 (edited) might give this one a go! Edited February 9, 2013 by splinter_1234 Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1411262 Share on other sites More sharing options...
timothyarden Posted February 9, 2013 Share Posted February 9, 2013 Might also want to use an array for all of this stuff $EmailTo = "me@me.com"; $Subject = "Request Form"; $Name = Trim(stripslashes($_POST['name'])); $Company = Trim(stripslashes($_POST['company'])); $Tel = Trim(stripslashes($_POST['phone'])); $Email = Trim(stripslashes($_POST['email'])); $Website = Trim(stripslashes($_POST['website'])); $Meeting = $_POST['meeting']; $Services = Implode("\n", $_POST['services']); $Message = Trim(stripslashes($_POST['message'])); $mailheader .= "Reply-To: $Email \r\n"; Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1411386 Share on other sites More sharing options...
Christian F. Posted February 10, 2013 Share Posted February 10, 2013 Timothy: What do you mean? Are you saying that he should have used an array to store the values retrieved from the POST array, or something else? In any case, why would you pick the absolute worst implementation to quote from? It is appreciated that you're trying to help, but please ensure that the help you're giving is using the best available code. Otherwise you're not helping, but quite the opposite. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1411416 Share on other sites More sharing options...
timothyarden Posted February 10, 2013 Share Posted February 10, 2013 What I meant was for all of these that he is giving individual variable names why not just use an array? In response to "is using the best available code" That was his code that was his code that I was suggesting he used an array for. Example array('EmailTo' => 'email value he had' ....... Quote Link to comment https://forums.phpfreaks.com/topic/271687-php-really-simple-form-validation-help/#findComment-1411420 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.