denno020 Posted January 7, 2012 Share Posted January 7, 2012 I am stumped at how to try and figure my problem out.. I've got a form that is checked using jQuery to make sure all fields are entered, and also checked in php after being submitted, again to make sure there was something in the fields. I ran through this tutorial http://net.tutsplus.com/tutorials/javascript-ajax/submit-a-form-without-page-refresh-using-jquery/, so that's basically the code I am using. I would like to be able to display a message to the user if they somehow managed to submit the form, and get past the javascript form validation without anything in a field.. How could I do this? I wasn't really sure how to word my problem, so searching google for help is a bit tricky as I don't really know what I'm searching for.. Thanks, Denno Quote Link to comment Share on other sites More sharing options...
Vel Posted January 7, 2012 Share Posted January 7, 2012 Saying this is basically the code I'm using isn't much help. Please post the actual code you are using. I'm sure you've made modifications to that code in the tutorial so it fits in with your site, we need to see what they are. Quote Link to comment Share on other sites More sharing options...
denno020 Posted January 8, 2012 Author Share Posted January 8, 2012 add_testimonial.php <?php <script> $(function() { $('.error').hide(); $(".button").click(function() { // validate and process form here $('.error').hide(); var name = $("input#name").val(); if (name == "") { $("label#name_error").show(); $("input#name").focus(); return false; } var email = $("input#email").val(); if (email == "") { $("label#email_error").show(); $("input#email").focus(); return false; } var testimonial = $("textarea#testimonial").val(); if (testimonial == "") { $("label#testimonial_error").show(); $("input#testimonial").focus(); return false; } var dataString = 'name='+ name + '&email=' + email + '&testimonial=' + testimonial; //alert (dataString);return false; $.ajax({ type: "POST", url: "scripts/parse_testimonial.php", data: dataString, success: function() { $('#contactForm').html("<div id='message'></div>"); $('#message').html("<h2>Contact Form Submitted!</h2>") .append("<p>We will be in touch soon.</p>") .hide() .fadeIn(1500, function() { $('#message').append(""); }); } }); return false; }); }); </script> <form name="add_testimonial_form" id="add_testimonial_form" action="" method="POST"> <label for="name" class="error" id="name_error">Please Enter A Name</label> <div><input type="text" name="name" id="name" placeholder="Name"/></div> <label for="email" class="error" id="email_error">Please Enter An Email</label> <div><input type="email" name="email" id="email" placeholder="Email" /></div> <label for="testimonial" class="error" id="testimonial_error">Please Enter A Testimonial</label> <div><textarea name="testimonial" id="testimonial" rows="5" placeholder="Testimonial"></textarea></div> <div><input type="submit" name="submit" class="button" id="submit_btn" value="Send" /></div> </form> ?> parse_testimonial.php <?php require_once "scripts/connect_to_mysql.php"; function cleanInput($data){ $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } //if(isset($_POST['submit'])){ $statusMessage = ""; $name = ""; $email = ""; $testimonial = ""; //check the form fields for stuff having been entered if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) { $name = cleanInput(strip_tags($_POST['name'])); } else {$name = 'No name entered';} if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) { $email = cleanInput(strip_tags($_POST['email'])); } else {$email = 'No email entered';} if ((isset($_POST['testimonial'])) && (strlen(trim($_POST['testimonial'])) > 0)) { $testimonial = cleanInput(strip_tags($_POST['testimonial'])); } else {$testimonial = 'No testimonial entered';} //Build query string to insert new data. //$sqlCommand = "INSERT INTO testimonials(name, email, date, testimonial) VALUES ('$name', '$email', curdate(), '$testimonial')"; //mysql_query($sqlCommand, $myConnection) or die (mysql_error()); //echo "Thanks"; //Confirm the sending on the form. exit(); //Don't display the form again after one has just been submitted. } //display error message on fields that aren't correctly filled in //if all correctly filled in, then submit the form and thank the user for their input //echo $statusMessage; //} ?> inside index.php, I use a php include to put the contents of add_testimonial.php inside the "contactForm" div. My code works perfectly, however I would like to be able to have a success/fail message passed back from parse_testimonial.php. (I added the opening and closing php tags in the post for add_testimonial.php just so that the code would be highlighted, it's not in my actual code). Thanks, Denno Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.