This question already has an answer here:
- How to show or hide a form using php? 5 answers
I have a contact form that I would like to hide after someone has completed and submitted their information. Below is the code. Ideally, I would like to hide everything between the HTML fieldset tags. I know this can be done with jQuery, but is it possible with PHP?
<form id="contact" method="post" action="index.php">
<fieldset>
<label for="name">Full Name<span class="red"> *</span></label>
<input type="text" name="name" title="Enter your name" required>
<label for="email">E-mail Address<span class="red"> *</span></label>
<input type="email" name="email" placeholder="yourname@domain.com" title="Enter your e-mail address" required>
<label for="phone">Phone Number</label>
<input type="tel" name="phone" title="Enter your phone number" placeholder="ex. (555) 555-5555">
<label for="message">Questions and Comments<span class="red"> *</span></label>
<textarea type="text" name="message" title="Enter your questions or comments" required></textarea>
<label>What is 4+2? (Anti-spam)<span class="red"> *</span></label>
<input type="text" name="human" placeholder="Answer Here" title="Answer Here" required>
<input type="submit" name="submit" class="button" id="submit" value="Send Us A Message" title="Send us a message" />
<p class="red">
* Indicates the field is required.
</p>
</fieldset>
<!-- Contact Form Details -->
<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'This message has been sent from your website.';
$to = 'user@domain.com';
$subject = 'From your contact form';
$human = $_POST['human'];
$body = " From: $name
E-Mail: $email
Message:
$message";
if ($_POST['submit'] && $human == '6') {
if (mail($to, $subject, $body, $from)) {
echo '
<h2>Thank You!</h2>
<span class="message">
Your message has been sent!
</span>';
} else {
echo '
<h2>Oops!</h2>
<span class="message">
Something went wrong, go back and try again!
</span>';
}
} else if ($_POST['submit'] && $human != '6') {
echo '
<h2>Incorrect Answer!</h2>
<span class="message">
The correct answer is 6. Please try again.
</span>';
}
?>
</form>
UPDATE: After trying a few suggestions, I realized that since the page was reloading after the form was submitted, trying to hide the form was pointless. You hit submit, form disappears, page reloads, form is there again. So I surrounded the from in a container and modified the CSS, so the message appears above the form, which is fine. Thank you all for the help though. It's appreciated!
</div>