HTML: I'm trying to use then integrate a form template, but when I hit submit it just goes to the PHP file. Help fixing this? -
form template origin: http://www.html-form-guide.com/contact-form/php-email-contact-form.html
html form page:
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>contact us</title> <!-- define style elements--> <style> h1 { font-family : arial, helvetica, sans-serif; font-size : 16px; font-weight : bold; } label,a { font-family : arial, helvetica, sans-serif; font-size : 12px; } </style> <!-- helper script vaidating form--> <script language="javascript" src="scripts/gen_validatorv31.js" type="text/javascript"></script> </head> </head> <body> <h1>contact us</h1> <form method="post" name="contactform" action="contact-form-handler.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"> <br> </p> <p> <label for='message'>message:</label> <br> <textarea name="message"></textarea> </p> <input type="submit" value="submit"><br> </form> <script language="javascript"> // code validating form // visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml // details var frmvalidator = new validator("contactform"); frmvalidator.addvalidation("name","req","please provide name"); frmvalidator.addvalidation("email","req","please provide email"); frmvalidator.addvalidation("email","email","please enter valid email address"); </script> <!-- sample code from: http://www.html-form-guide.com/contact-form/php-email-contact-form.html --> </body> </html>
php script:
<?php $errors = ''; $myemail = 'yourname@website.com';//<-----put email address here. if(empty($_post['name']) || empty($_post['email']) || empty($_post['message'])) { $errors .= "\n error: fields required"; } $name = $_post['name']; $email_address = $_post['email']; $message = $_post['message']; if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n error: invalid email address"; } if( empty($errors)) { $to = $myemail; $email_subject = "contact form submission: $name"; $email_body = "you have received new message. ". " here details:\n name: $name \n email: $email_address \n message \n $message"; $headers = "from: $myemail\n"; $headers .= "reply-to: $email_address"; mail($to,$email_subject,$email_body,$headers); //redirect 'thank you' page header('location: contact-form-thank-you.html'); } ?> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <title>contact form handler</title> </head> <body> <!-- page displayed if there error --> <?php echo nl2br($errors); ?> </body> </html>
details: when type in details click submit, redirected php script in browser. can please have guidance have researched quite bit , don't know php. hope that's enough details!
if don't want form redirect on php page need use ajax submit form , response php file. give form id (suppose id contactform)
// jquery code
<script> $("#contactform").submit(function(event){ event.preventdefault(); $.ajax({ method: "post", url: "contact-form-handler.php", data: { name: $("#name"), email: $("#email"),message: $("#message") }, success: function(response){ alter(response.data); } }) }) </script>
// php- code
$errors = ''; $myemail = 'yourname@website.com';//<-----put email address here. if(empty($_post['name']) || empty($_post['email']) || empty($_post['message'])) { $errors .= "\n error: fields required"; } $name = $_post['name']; $email_address = $_post['email']; $message = $_post['message']; if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n error: invalid email address"; } if( empty($errors)) { $to = $myemail; $email_subject = "contact form submission: $name"; $email_body = "you have received new message. ". " here details:\n name: $name \n email: $email_address \n message \n $message"; $headers = "from: $myemail\n"; $headers .= "reply-to: $email_address"; mail($to,$email_subject,$email_body,$headers); //set success message, here have used variable name error can change per requirment. $error = 'mail sent !!'; } $data['error'] = $error; echo json_encode($data); exit();
Comments
Post a Comment