/ Published in: jQuery
URL: http://luiszuno.com/themes/watercolor/spring/contact.html
Validates a form before sending its values to a php file
Expand |
Embed | Plain Text
$(document).ready(function(){ // hide messages $("#error").hide(); $("#success").hide(); // on submit... $("#send").click(function() { $("#error").hide(); //required: //name var name = $("input#name").val(); if(name == ""){ $("#error").fadeIn().text("Name required."); $("input#name").focus(); return false; } var email = $("input#email").val(); if(email == ""){ $("#error").fadeIn().text("Email required"); $("input#email").focus(); return false; } // web var web = $("input#web").val(); if(web == ""){ $("#error").fadeIn().text("Web required"); $("input#web").focus(); return false; } // comments var comments = $("#comments").val(); // data string var dataString = 'name='+ name + '&email=' + email + '&web=' + web + '&comments=' + comments; // ajax $.ajax({ type:"POST", url: "send-mail.php", data: dataString, success: success() }); }); // on success... function success(){ $("#success").fadeIn(); $("#formMail").fadeOut(); } return false; }); // usage <!-- form --> <form id="formMail" action="#" method="post"> <fieldset> <p> <label>Name:</label> <input name="name" id="name" type="text" /> </p> <p> <label>Email:</label> <input name="email" id="email" type="text" /> </p> <p> <label>Web:</label> <input name="web" id="web" type="text" /> </p> <p> <label>Comments:</label> <textarea name="comments" id="comments" rows="5" cols="20" ></textarea> </p> <br/><br/> <p><input type="button" value=" " name="send" id="send" /></p> </fieldset> <p id="error">Message</p> </form> <p id="success">Thanks for your comments.</p> <!-- ENDS form -->
You need to login to post a comment.
