December 4, 201312 yr Trying to add a simple bit of validation to my contact form and it seems to not be checking the field. Anyone mind pointing out why that is? (It's a simple 2 + 2 at the moment). contact.php <?php error_reporting(E_ALL ^ E_NOTICE); // hide all basic notices from PHP //If the form is submitted if(isset($_POST['submitted'])) { // require a name from user if(trim($_POST['contactName']) === '') { $nameError = 'Forgot your name!'; $hasError = true; } else { $name = trim($_POST['contactName']); } // need valid email if(trim($_POST['email']) === '') { $emailError = 'Forgot to enter in your e-mail address.'; $hasError = true; } else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST['email']))) { $emailError = 'You entered an invalid email address.'; $hasError = true; } else { $email = trim($_POST['email']); } // we need at least some content if(trim($_POST['comments']) === '') { $commentError = 'You forgot to enter a message!'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } // we need at least some content if(trim($_POST['comments']) === '') { $commentError = 'You forgot to enter a message!'; $hasError = true; } else { if(function_exists('stripslashes')) { $comments = stripslashes(trim($_POST['comments'])); } else { $comments = trim($_POST['comments']); } } // THIS SECTION if(isset($_POST['validation']) && $_POST['validation'] == 4){ $validationError = 'Please enter a number!'; $validationError = true; } else { $validation = trim($_POST['validation']); } // END SECTION // upon no failure errors let's email now! if(!isset($hasError)) { $emailTo = 'email@email.com'; $subject = "$name"; $sendCopy = trim($_POST['sendCopy']); $body = "Name: $name \n\nEmail: $email \n\nComments: $comments"; $headers = 'From: ' .' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email; mail($emailTo, $subject, $body, $headers); // set our boolean completion value to TRUE $emailSent = true; } } ?> Form <?php if(isset($emailSent) && $emailSent == true) { ?> <p class="info">Your email was sent.</p> <?php } else { ?> <div id="contact-form"> <?php if(isset($hasError) || isset($captchaError) ) { ?> <p class="alert">Error submitting the form</p> <?php } ?> <form id="contact-us" action="index.php" method="post"> <div class="formblock"> <input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="txt requiredField" placeholder="Name" /> <?php if($nameError != '') { ?> <br /> <span class="error"><?php echo $nameError;?></span> <?php } ?> </div> <div class="formblock"> <input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="txt requiredField email" placeholder="Email" /> <?php if($emailError != '') { ?> <br /> <span class="error"><?php echo $emailError;?></span> <?php } ?> </div> <div class="formblock"> <textarea name="comments" id="commentsText" class="txtarea requiredField" placeholder="Message"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?> </textarea> <?php if($commentError != '') { ?> <br /> <span class="error"><?php echo $commentError;?></span> <?php } ?> </div> <div class="formblock"> <input type="text" name="validation" id="validation" value="<?php if(isset($_POST['validation'])) echo $_POST['validation'];?>" class="txt requiredField validation" placeholder="2 + 2" /> <?php if($validationError != '') { ?> <br /> <span class="error"><?php echo $validationError;?></span> <?php } ?> </div> <button name="submit" type="submit" class="subbutton">Send us Mail!</button> <input type="hidden" name="submitted" id="submitted" value="true" /> </form> </div> <?php } ?> Thanks for any help (:
December 5, 201312 yr Your setting $validationError twice, $hasError and $validationError would seam correct. if(isset($_POST['validation']) && $_POST['validation'] == 4){ $validationError = 'Please enter a number!'; $hasError = true; } else { $validation = trim($_POST['validation']); }
December 5, 201312 yr You should consider using PHP FILTER_VALIDATE_EMAIL instead of your long preg_match code. It's more modern and I assume it's kept up to date by PHP program developers. Your preg_match code only covers 4 characters in a filename extension, but some new domain extensions now have more characters. Edited December 5, 201312 yr by Wickham
December 5, 201312 yr Author Cheers for the advice, I've actually altered this to see if I can match a single word and I'm still having the same issue, it checks if it's empty, but still lets you put anything in there and doesn't check it... if(trim($_POST['val']) === '') { $valError = 'Forgot to enter in your validation.'; $hasError = true; } else if (!preg_match("/test/", trim($_POST['val']))) { $valError = 'You entered an invalid word.'; $hasError = true; } else { $val = trim($_POST['val']); } Something I'm missing? Cheers Wickham, got that implimented and it's working fine (: // need valid email if(trim($_POST['email']) === '') { $emailError = 'Forgot to enter in your e-mail address.'; $hasError = true; } else if (!filter_var($email, FILTER_VALIDATE_EMAIL, trim($_POST['email']))) { $emailError = 'You entered an invalid email address.'; $hasError = true; } else { $email = trim($_POST['email']); }
Create an account or sign in to comment