PHP form email validation -
my mail form still sending emails if email address not valid. example, if fill in email "bob", , hit submit, javascript validator gives warning message, email still goes through. ends in spam box bob@mydomain.com
how can validate email address, , prevent submit if not validate?
i new php.
html:
<div id="emailform"> <h2>confirm purchase information</h2> <hr> <form method="post" name="contactform" action="mail_form.php" id="submit"> <p> <label for='name'>your name:</label> <br> <input type="text" name="name"> </p> <p> <label for='email'>email address:</label> <br> <input type="text" name="email"> </p> <p> <label for='purchasecode'>purchase code:</label> <br> <input type="text" name="purchasecode"> </p> <p> <label for='vendor'>vendor name:</label> <br> <select name="vendor"> <option value="" selected="selected"></option> <option value="amazon" >amazon</option> <option value="barnes&noble" >barnes & noble</option> <option value="family christian" >family christian</option> <option value="christianbook" >christianbook.com</option> <option value="lifeway" >lifeway</option> <option value="bam" >books-a-million</option> <option value="mardel" >mardel</option> </select> </p> <button type="submit" id="submitbutton" name="submit" value="submit" class="mainbutton">submit</button><br> </form> <!-- code validating form visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml details --> <script type="text/javascript"> var frmvalidator = new validator("contactform"); frmvalidator.addvalidation("name","req","please provide name"); frmvalidator.addvalidation("email","email","please enter valid email address"); frmvalidator.addvalidation("vendor","dontselect=000"); frmvalidator.addvalidation("purchasecode","maxlen=50"); </script> </div>
php:
<?php ini_set('display_errors',1); error_reporting(e_all); if(!isset($_post['submit'])) { //this page should not accessed directly. need submit form. echo "error; need submit form!"; } $name = $_post['name']; $email = $_post['email']; $purchasecode = $_post['purchasecode']; $vendor = $_post['vendor']; //validate first if(empty($_post['name']) || empty($_post['email']) || empty($_post['purchasecode']) || empty($_post['vendor'])) { echo "all fields required."; exit; } if(isinjected($email)) { echo "bad email value!"; exit; } $email_from = $email; $email_subject = "gdfy purchase confirmation"; $email_body = "new purchase confirmation $name.\n". "here details:\n\n name: $name \n\n email: $email \n\n purchase code: $purchasecode \n\n vendor: $vendor"; $to = "idc615@gmail.com";//<== update email address $headers = "from: $email_from \r\n"; $headers .= "reply-to: $email_from \r\n"; //send email! mail($to,$email_subject,$email_body,$headers); //done. redirect thank-you page. header('location: index.html'); // echo "success"; // function validate against email injection attempts function isinjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0a+)', '(%0d+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?>
javascript:
$('#submit').submit(function() { // catch form's submit event $.ajax({ // create ajax call... data: $(this).serialize(), // form data type: $(this).attr('method'), // or post url: $(this).attr('action'), // file call success: function(response) { // on success.. $('#emailform').html("<h2 style='text-align:center;'>thank you!</h2><hr><p style='text-align:center;'>thank submitting purchase information.<br>we send free gifts soon!</p>"); // update div } }); return false; // cancel original event prevent form submitting });
you can use filter_var :
if( filter_var('bob@example.com', filter_validate_email) ) { do_stuff(); }
Comments
Post a Comment