php - Ajax and jQuery custom form submission in Wordpress -
i'm not tech completely, , i'm trying build custom theme wordpress. so, came point need implement custom js script send form data. far understand, it's going php file, i'm concentrated on front-end. ajax + jquery validation. don't want form refresh page after sends data, simple message telling went successful.
can have @ code wrote , tell me what's wrong it? took me 2 days..
ps - file, stores code embedded wp theme properly, jquery dependancy. wonder, have implement ajax, or comes jquery?
http://codepen.io/anon/pen/mpdrpe
<form class="form"> <div class="form__item form__item_no-margin"> <input type="text" name="firstname" placeholder="what's name?*" class="firstname" required> <p class="error-message">sorry, field can't empty.</p> </div> <div class="form__item"> <input type="text" name="email" placeholder="what's email address?*" class="email" required> <p class="error-message">oopps, haven't seen emails that.</p> </div> <div class="form__item"> <textarea name="comment" placeholder="want leave message?*" class="textarea" required></textarea> <p class="error-message">nothing @ all? really?</p> </div> <div class="form__item"> <input type="button" name="submit" value="send" class="submit-btn"> <p class="error-message error-message_main val-error">all required fields have filled out.</p> <p class="error-message error-message_main_success val-success">thanks. i'll contact asap!</p> </div> </form> .error-message { display: none; } jquery(document).ready(function(){ jquery(".submit-btn").click(function(){ var name = jquery(".firstname").val(); var email = jquery(".email").val(); var message = jquery(".textarea").val(); if(name === "" || email === "" || message === "") { jquery(".val-error", ".error-message").css("display", "block"); } else { jquery.ajax({ url:"/assets/php/send.php", method:"post", data:{name:firstname, email:email, message:comment}, success: function(data) { jquery("form").trigger("reset"); jquery(".val-success").show(fast); } }); } }); });
first need prevent default click event
second need action variable pass wordpress hook
3th jquery selector showing errors incorrect, coma needs in string
jquery(document).ready(function(){ jquery(".submit-btn").click(function(e){ e.preventdefault(); var name = jquery(".firstname").val(); var email = jquery(".email").val(); var message = jquery(".textarea").val(); if(name === "" || email === "" || message === "") { jquery(".val-error, .error-message").show();//a little bit cleaner } else { jquery.ajax({ url:"/assets/php/send.php", method:"post", data:{name:firstname, email:email, message:comment,action:'validate_form'}, success: function(data) { jquery("form").trigger("reset"); jquery(".val-success").show(fast); } }); } }); });
for more information read wp documentation on ajax
Comments
Post a Comment