php mail form submit - 500 error -
one of first php forms, , i'm having trouble. form not submit. i'm sure combination of problems. feel i've used appropriate id tags , such. not sure if problem redirect, or mail function, or validator.
html:
<div id="emailform"> <h2>confirm purchase information</h2> <hr> <form method="post" name="contactform" action="mail_form.php"> <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="1" selected="selected"></option> <option value="2" >amazon</option> <option value="3" >barnes & noble</option> <option value="4" >family christian</option> <option value="5" >christianbook.com</option> <option value="6" >lifeway</option> <option value="7" >books-a-million</option> <option value="8" >mardel</option> </select> </p> <button type="submit" id="submitbutton" value="submit" class="mainbutton">submit</button><br> </form>
php:
<?php 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($visitor_email)) { echo "bad email value!"; exit; } $email_from = $email; $email_subject = "new form submission"; $email_body = "you have received new message user $name.\n". " here details:\n name: $name \n email: $email \n purchase code \n $purchasecode \n vendor \n $vendor";. $to = "name@domain.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'); // 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; } } ?>
there 2 errors in code actually.
1) dot @ end of:
$email_body = "you have received new message user $name.\n". " here details:\n name: $name \n email: $email \n purchase code \n $purchasecode \n vendor \n $vendor";.
which needs replaced by:
$email_body = "you have received new message user $name.\n". " here details:\n name: $name \n email: $email \n purchase code \n $purchasecode \n vendor \n $vendor";
2) submit button not named, therefore show error message error; need submit form
when form sent.
solution:
<button type="submit" name="submit" id="submitbutton" value="submit" class="mainbutton">submit</button><br>
fully tested code
html form
<div id="emailform"> <h2>confirm purchase information</h2> <hr> <form method="post" name="contactform" action="mail_form.php"> <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="1" selected="selected"></option> <option value="2" >amazon</option> <option value="3" >barnes & noble</option> <option value="4" >family christian</option> <option value="5" >christianbook.com</option> <option value="6" >lifeway</option> <option value="7" >books-a-million</option> <option value="8" >mardel</option> </select> </p> <button type="submit" name="submit" id="submitbutton" value="submit" class="mainbutton">submit</button><br> </form>
php handler
<?php 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($visitor_email)) { echo "bad email value!"; exit; } $email_from = $email; $email_subject = "new form submission"; $email_body = "you have received new message user $name.\n". " here details:\n name: $name \n email: $email \n purchase code \n $purchasecode \n vendor \n $vendor"; $to = "name@domain.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; } } ?>
Comments
Post a Comment