jquery - Ajax form uses name field data in success message when in JSFIDDLE but doesn't work on my site -
i using form displays success message person's name, achieved using data filled in name input field of form.
the form successful when viewing within jsfiddle, here.
but when using code on site, success message says: "hi undefined" instead of "hi john appleseed", example.
how success message display input data first field in form?
thanks.
jquery
$(document).ready(function() { var errors = false; $('.required').parent().find('.input').on('blur', function() { var error_div = $(this).parent().find('.error_message'); var field_container = $(this).parent(); if (!$.empty_field_validation($(this).val())) { error_div.html('this field required.'); error_div.css('display', 'block'); field_container.addclass('error'); errors = true; } else { error_div.html(''); error_div.css('display', 'none'); field_container.removeclass('error'); errors = false; } }); $('#email').on('blur', function() { var error_div = $(this).parent().find('.error_message'); var field_container = $(this).parent(); if (!$.email_validation($(this).val())) { error_div.html('expected input: email'); error_div.css('display', 'block'); field_container.addclass('error'); errors = true; } else { error_div.html(''); error_div.css('display', 'none'); field_container.removeclass('error'); errors = false; } }); $('#message').on('blur', function() { var error_div = $(this).parent().find('.error_message'); var field_container = $(this).parent(); if (!$.empty_field_validation($(this).val())) { error_div.html('expected input: message'); error_div.css('display', 'block'); field_container.addclass('error'); errors = true; } else { error_div.html(''); error_div.css('display', 'none'); field_container.removeclass('error'); errors = false; } }); $('#contact_form').submit(function(event) { event.preventdefault(); $('.required').parent().find('.input').trigger('blur'); if (!errors) $.ajax({ url: '/echo/json/', 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'); } }); else alert("you didn't completed form correctly. check out , try again!"); }); }); $.empty_field_validation = function(field_value) { if (field_value.trim() == '') return false; return true; } $.email_validation = function(email) { var regex = /^([a-za-z0-9_.+-])+\@(([a-za-z0-9-])+\.)+([a-za-z0-9]{2,4})+$/; return regex.test(email); } $.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; }; form
<div id="after_submit"></div> <form id="contact_form" action="#" method="post" enctype="multipart/form-data"> <div class="row"> <label class="required" for="name">your name:</label> <br /> <input id="name" class="input" name="name" type="text" value="" size="30" /> <br /> <span id="name_validation" class="error_message"></span> </div> <div class="row"> <label class="required" for="email">your email:</label> <br /> <input id="email" class="input" name="email" type="text" value="" size="30" /> <br /> <span id="email_validation" class="error_message"></span> </div> <div class="row"> <label class="required" for="message">your message:</label> <br /> <textarea id="message" class="input" name="message" rows="7" cols="30"></textarea> <br /> <span id="message_validation" class="error_message"></span> </div> <input id="submit_button" type="submit" value="send email" /> </form> 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: red; } .error { background-color: #ffdfdf; color: red; } .error_message { font-style: italic; font-size: 10px; } .row { margin: 5px; }
in function:
success: function(data) { var message = 'hi ' + data.name + '. message sent , received.'; //... } if data.name undefined means whatever server returned doesn't have name property. (if server returned valid json object @ all.) you're going have debug , see server does return. , check server-side code (which don't show here) see what's going on.
in function:
error: function() { var message = 'hi ' + data.name + '. message not sent or received. please try again later'; //... } there is no data variable defined in function. data undefined. i'm not sure expect data.name contain here, or think it's coming from, since never defined doesn't exist.
Comments
Post a Comment