javascript - Form's embedded jQuery Validate link creates an issue sending email, but if creates other issues if moved within file -
i using form validated jquery validate , general validation.
currently, having 2 issues:
the hierarchy of files have been embedded on page follows:
- (1) php
- (2) jquery-1.11.3.js
- (3) jquery.validate.js
- (4) jquery validation scripts
issue (a) although fields correctly validated , 'red' class added incorrect input fields, email sends success message doesn't appear.
if change hierarchy of embedded files to:
- (1) php
- (2) jquery-1.11.3.js
- (4) jquery validation scripts
- (3) jquery.validate.js
issue (b) although fields correctly validated, 'red' class no longer gets added of input fields. also, success message appears form sends email has no inputted data.
how around these issues form works way should (in issue a) sends email data in message?
so clarify, want form work way in issue (a), problem email being sent blank. if change hierarchy of embedded files, email sends data success message doesn't appear.
thanks.
html
<div id="after_submit"></div> <form id="contact_form" action="" method="post" enctype="multipart/form-data"> <div id="form-container" class="row form-input-group"> <input id="name" class="input required" name="name" type="text" value="" size="30"/><br /> </div> <div id="form-container" class="row form-input-group"> <input id="email" class="input required" name="email" type="email" value="" size="30"/><br /> </div> <div id="form-container" class="row form-input-group"> <input id="telephone" class="input" name="telephone" type='tel'><br /> </div> <div id="form-container" class="row form-input-group"> <input id="event_date" class="input required" name="event_date" type="text" value="" size="30"/><br /> </div> <div id="form-container" class="row form-input-group"> <input id="guests" class="input required" name="guests" type="text" value="" size="30"/><br /> </div> <div id="form-container" class="row form-input-group"> <input id="hear_about" class="input required" name="hear_about" type="text" value="" size="30"/><br /> </div> <div id="form-container" class="row form-input-group"> <textarea id="message" class="input required" name="message" rows="7" cols="30"></textarea><br /> </div> <input id="submit_button" type="submit" value="send email" /> </form>
php
<?php if ($_server['request_method'] === 'post') { $subject = 'website contact form submission'; $message = " <html> <head> </head> <body> <table> <tr><td>name:</td><td style=\"color: red;\"> ".$_post["name"]."</td></tr> <tr><td>email:</td><td style=\"color: red;\"> ".$_post["email"]."</td></tr> <tr><td>telephone:</td><td style=\"color: red;\"> ".$_post["telephone"]."</td></tr> <tr><td>event date:</td><td style=\"color: red;\"> ".$_post["event_date"]."</td></tr> <tr><td>guests amount:</td><td style=\"color: red;\"> ".$_post["guests"]."</td></tr> <tr><td>hear about:</td><td style=\"color: red;\"> ".$_post["hear_about"]."</td></tr> <tr><td>message:</td><td style=\"color: red;\"> ".$_post["message"]."</td></tr> </table> </body> </html> "; $headers = "mime-version: 1.0" . "\r\n"; $headers .= "content-type:text/html;charset=utf-8" . "\r\n"; $headers .= "from: ".$_post["name"]." <".$_post["email"].">\r\n"; $headers .= "reply-to: ".$_post["name"]." <".$_post["email"].">\r\n"; mail("example@example.com",$subject,$message,$headers); $nameerr = $emailerr = $telephoneerr = $hear_abouterr = $messageerr = ""; $name = $email = $telephone = $event_date = $guests = $hear_about = $message = ""; $name = $_post['name']; // required $email = $_post['email']; // required $telephone = $_post['telephone']; // not required $event_date = $_post['event_date']; // not required $guests = $_post['guests']; // not required $hear_about = $_post['hear_about']; // required $message = $_post['message']; // required // required if ($_server["request_method"] == "post") { if (empty($_post["name"])) { $nameerr = "name required"; } else { $name = test_input($_post["name"]); // check if name contains letters , whitespace if (!preg_match("/^[a-za-z ]*$/",$name)) { $nameerr = "only letters , white space allowed"; } } // required if (empty($_post["email"])) { $emailerr = "email required"; } else { $email = test_input($_post["email"]); // check if e-mail address well-formed if (!filter_var($email, filter_validate_email)) { $emailerr = "invalid email format"; } } // not required if (empty($_post["telephone"])) { $telephoneerr = "incorrect telephone format"; } else { $telephone = test_input($_post["telephone"]); // check if telephone well-formed if ( preg_match( '/^[+]?([\d]{0,3})?[\(\.\-\s]?([\d]{3})[\)\.\-\s]*([\d]{3})[\.\-\s]?([\d]{4})$/', $string ) ) { $telephoneerr = "invalid telephone format"; } } // not required if (empty($_post["event_date"])) { $event_date = ""; } else { $event_date = test_input($_post["event_date"]); } // not required if (empty($_post["guests"])) { $guests = ""; } else { $guests = test_input($_post["guests"]); } // required if (empty($_post["hear_about"]) < 5) { $hear_abouterr = "please let know heard us"; } else { $hear_about = test_input($_post["hear_about"]); } // required if (empty($_post["message"]) < 5) { $messageerr = "not enough characters"; } else { $message = test_input($_post["message"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } } ?>
other jquery validations
$(document).ready(function(){ debug: true, $("#contact_form").validate({ rules: { name: { required: true, minlength: 5, letters : true, }, email: { email: true, }, telephone: { required: false, digits: true, minlength: 10, }, message: { required: true, minlength: 5, } }, messages: { name: { required: "please enter name", minlength: "name not long enough", letters: "please enter characters only" }, email: { required: "please enter valid email address" }, message: { required: "please enter message", minlength: "please enter @ least 5 characters" }, telephone: { digits: "please enter valid phone number", minlength: "phone number not long enough" } }, errorelement : 'span', errorclass:'error', validclass:'success', highlight: function (element, errorclass, validclass) { $(element).parents("#form-container").addclass(errorclass).removeclass(validclass); }, unhighlight: function (element, errorclass, validclass) { $(element).parents("#form-container").removeclass(errorclass).addclass(validclass); }, }); var errors = false; $('.required').parent().find('.input').on('blur', function() { var field_container = $(this).parent(); if (!$.empty_field_validation($(this).val())) { errors = true; } else { field_container.removeclass('error'); errors = false; } }); $('#name').on('blur', function(){ var field_container = $(this).parent(); if (!$.empty_field_validation($(this).val())) { $("#name").addclass("red"); errors = true; } else { $("#name").removeclass("red"); errors = false; } }); $('#message').on('blur', function(){ var field_container = $(this).parent(); if (!$.empty_field_validation($(this).val())) { $("#message").addclass("red"); errors = true; } else { $("#message").removeclass("red"); errors = false; } }); $('#email').on('blur', function(){ var field_container = $(this).parent(); if (!$.email_validation($(this).val())) { $("#email").addclass("red"); errors = true; } else { $("#email").removeclass("red"); errors = false; } }); $('#telephone').on('blur', function(){ var field_container = $(this).parent(); if (!$.tel_validation($(this).val())) { $("#telephone").addclass("red"); errors = true; } else { $("#telephone").removeclass("red"); errors = false; } }); $('#contact_form').submit(function(event) { event.preventdefault(); $('.required').parent().find('.input').trigger('blur'); if (!errors) $.ajax({ url: "index.php", data: { json: json.stringify($(this).serializeobject()) }, type: 'post', success: function(data) { var message = 'hi '+data.name+'. message sent , received.'; $('#after_submit').html(message); $('#after_submit').css('display', 'block'); }, error: function() { var message = 'hi '+data.name+'. message not sent or received. please try again later'; $('#after_submit').html(message); $('#after_submit').css('display', 'block'); } }); }); }); $.empty_field_validation = function(field_value) { if (field_value.trim() == '') return false; return true; } $.validator.addmethod('letters', function(value) { //custom method validator return value.match(/^[- a-za-z]+$/); }); $.email_validation = function(email) { var regex = /^([a-za-z0-9_.+-])+\@(([a-za-z0-9-])+\.)+([a-za-z0-9]{2,4})+$/; return regex.test(email); } $.tel_validation = function(telephone) { var regex = /^\d{3}(?: ?\d+)*$/; return regex.test(telephone); } $.fn.serializeobject = function() { var o = {}; var = this.serializearray(); $.each(a, function() { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; };
css
body{ width: 414px; font-family: arial; font-size: 14px; } #after_submit, #email_validation, #name_validation { display:none; } #after_submit{ background-color: #c0ffc0; line-height: 31px; margin-bottom: 10px; padding-left: 20px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; } label, #after_submit{ color: #6c6c6c; } input{ line-height: 31px; } input, textarea{ width: 288px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; background-color: rgba(255,255,255,.6); border: solid 1px #b6c7cb; } #contact_form{ height: 317px; background-color: #e1e9eb; border: solid 1px #e5e5e5; padding: 10px 20px 50px 20px; -webkit-border-radius: 5px; -moz-border-radius: 5px; border-radius: 5px; } #submit_button{ width: 109px; height: 34px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px; background-color: #86c5fa; -webkit-box-shadow: inset 0 2px rgba(255,255,255,.29); -moz-box-shadow: inset 0 2px rgba(255,255,255,.29); box-shadow: inset 0 2px rgba(255,255,255,.29); border: solid 1px #77a4cb; font-weight: bold; color: #fff; margin-left: 7px; } label.required:after { content:'*'; color:#cb2027; } .red { color: #cb2027; border: 1px solid #cb2027; } .error_message{ font-style: italic; font-size: 10px; } .row { margin:5px; } #email-error, #message-error, #name-error, #telephone-error { background: #fff none repeat scroll 0 0; border: medium none; color: #cb2027; display: block; float: left; margin-left: auto; margin-right: auto; padding: 8px; width: 288px; }
Comments
Post a Comment