Andy626 Posted June 13, 2013 Share Posted June 13, 2013 (edited) Hi All, Please excuse the newbie question but I am struggling with this one. I'm just after some advice on what code to put in and where to make these eight fields mandatory to fill out in the contact page of our site. I've found a lot of sites with code to put in but none seem to work. Apologies for the basic question and appreciate any help given! Here is the current form... (Please ignore the promo and Toybox area as this was used for our sister company in the states) <form method="post" action="db_send-contact-form.php"> <p class="style17">Your Name: <br /> <textarea name="name" rows="1" cols="40" id="name"></textarea> </p> <p class="style22">Company: <br /> <textarea name="company" rows="1" cols="40" id="company"></textarea> </p> <p class="style22">Email:<br /> <textarea name="email" rows="1" cols="40" id="email"></textarea> </p> <p class="style22">When are you looking at having this installed?<br /> <textarea name="when" rows="1" cols="40" id="when"></textarea> </p> <p class="style22">Do you have a Product Code in mind?<br /> <input type='text' name="code" style='width: 300px' id="code" value="<?PHP if(isset($_REQUEST['code'])) { echo $_REQUEST['code']; } if(isset($_REQUEST['toybox'])) { if($_REQUEST['toybox'] == 1) { echo "ToyBox: ". $_COOKIE['ToyBox']; } else { $uID = ''; if(isset($_SESSION['uID'])) { $uID = $_SESSION['uID']; } echo "ToyBox: ". db_getToybox($_REQUEST['toybox'], $uID); } } if($_SESSION['Promotion'] == "PROMO2012APR") { echo " | MUST INCLUDE SALE PRICE"; } ?>" /> </p> <p class="style22">Phone:<br /> <textarea name="phone" rows="1" cols="40" id="phone"></textarea> </p> <p class="style22">City & State:<br /> <textarea name="state" rows="1" cols="40" id="state"></textarea> </p> <p class="style22">Any other info?<br /> <textarea name="message" rows="15" cols="40" id="message"></textarea> </p> <p><br> <input type="submit"> </p> </form> Edited June 13, 2013 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/279097-making-mandatory-fields-in-contact-form/ Share on other sites More sharing options...
DaveyK Posted June 13, 2013 Share Posted June 13, 2013 You should seperate your pages logic from the HTML, meaning you shouldnt be processing PHP in your HTML, but merely displaying it. Try to move all those IF statements to the top, like so: <?php if () { (...) } ?> <html> </html> Quote Link to comment https://forums.phpfreaks.com/topic/279097-making-mandatory-fields-in-contact-form/#findComment-1435685 Share on other sites More sharing options...
gizmola Posted June 13, 2013 Share Posted June 13, 2013 With all due respect if you don't understand the most basic elements of the PHP language, there's not much anyone can do for you here. That gobbledegook of code you posted seems to indicate as much. There is a lot to form handling and validation ... so much to it that in frameworks, form classes are typically some of the most complex components included. For example, a simple way to do it would be to write a "validateForm" function that includes a whitelist of required form names. function validateForm($required) { //$required = array('name', 'company', 'email'); $errors = array(); foreach ($required as $name) { if (!isset($_POST[$name])) { $errors[] = "$name is required"; } elseif (trim($_POST[$name]) == '') { $errors[] = "$name can not be blank"; } } return $errors; } You could call this function in your form processing section and check whether count($errors) > 0. If so, you output the form again, typically including a div with a list of errors. Needless to say you could add much more code to validate specific fields in a particular way (check that email is in a valid format, check that names have at least a certain number of characters, etc) but I'm not going to go overboard in this reply. If you could integrate the snippet I provided you might be able to get this done, however if not, you really need to get the basics under your belt, or find someone more experienced to write it for you. Quote Link to comment https://forums.phpfreaks.com/topic/279097-making-mandatory-fields-in-contact-form/#findComment-1435686 Share on other sites More sharing options...
fastsol Posted July 11, 2013 Share Posted July 11, 2013 (edited) As stated previously, form validation can be tricky and complicated. I have made a tutorial on this subject that should help you. http://amecms.com/article/How-to-validate-a-form-the-right-way Edited July 11, 2013 by fastsol Quote Link to comment https://forums.phpfreaks.com/topic/279097-making-mandatory-fields-in-contact-form/#findComment-1440259 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.