jQuery - Hide form on Submit
I would like to hide my form on successful submission:
Here is a link to the testspace: http://www.bgv.co.za/testspace/contact_3.php
Its a combination PHP jQuery mongrel. At the moment I'm using jQuery validator and I have added custom script to change the class on the input fields wrapping div - as a way to show required fields.
Here is what I have in the PHP
<?php
$subject = "Website Contact Form Enquiry";
//If the form is submitted if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['message']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
开发者_JAVA百科 }
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'info@bgv.co.za'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
} ?>
and here is the script area:
$(document).ready(function(){
$('#contactform').validate({
showErrors: function(errorMap, errorList) {
//restore the normal look
$('#contactform div._required').removeClass('_required').addClass('xrequired');
//stop if everything is ok
if (errorList.length == 0) return;
//Iterate over the errors
for(var i = 0;i < errorList.length; i++)
$(errorList[i].element).parent().removeClass('xrequired').addClass('_required');
}
}); });
So at the moment when someone submits a valid form you see a new heading just under Contact form - its from here:
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<h1 class="success_form">Thank You!</h1>
<?php } ?>
and below it is the data entered into the form.
But its the form shoing below that I want to hide.... Please help :)
Here is the new bit I added thanks to Flashbackzoo
$("#content").empty();
$("#content").append(
"<p>If you want to be kept in the loop...</p>" +
"<p>Or you can contact...</p>"
);
$('h1.success_').removeClass('success_').addClass('success_form');
$('#contactform').hide();
},
Add a submit handler...
$('#contactform').validate({
showErrors: function(errorMap, errorList) {
// your code...
},
submitHandler: function() {
$('#contactform').hide();
}
});
UPDATE
In response to your comment Brett. You could change your submitHandler to something like...
submitHandler: function() {
$("#content").empty();
$("#content").append(
"<p>If you want to be kept in the loop...</p>" +
"<p>Or you can contact...</p>"
);
}
This will remove all child elements from #content using the .empty() method. Then add elements to #content using the .append() method. You can swap out #content for any selector you like.
精彩评论