提交后隐藏电子邮件表单

I have a PHP email form embedded at the top of the HTML of my contact form page (index.php):

<?php
    if (isset($_POST["submit"])) {
        $name = $_POST['name'];
        $email = $_POST['email'];
        $message = $_POST['message'];
        $from = 'Camino Contact Form'; 
        $to = 'email@example.com'; 
        $subject = 'Message Contact Form ';     
        $body ="From: $name
 E-Mail: $email
 Message:
 $message";

if (!$errName && !$errEmail && !$errMessage) {
    if (mail ($to, $subject, $body, $from)) {
        $result='<div>Thank You! I will be in touch</div>';
    } else {
        $result='<div>Sorry there was an error sending your message.</div>';
    }
}
    }
?>

HTML:

<form role="form" method="post" action="index.php">
    ...
</form>

and I wanted to hide the form only after successful submission. How can I do this?

In your CSS file;

.hide {
    display:none;
}

(If unsure how to do this, this question should help)

Then inside your PHP if block;

$class = "";
if (mail ($to, $subject, $body, $from)) {
        $result='<div>Thank You! I will be in touch</div>';
        $class = 'class="hide"';
    } else {
        $result='<div>Sorry there was an error sending your message.</div>';
    }

and finally, on index.php:

<form role="form" method="post" action="index.php" <?php echo $class ?>>

Now you can hide the entire form, with a little bit of CSS and a dynamic variable.

Edit:

If you wanted to avoid using an external or internal CSS file entirely, you could apply the CSS inline to your html element directly, like so;

$style = 'style = "display:none;"';

An alternative approach, if you're not using style sheets or any other CSS.

To hide form after submission and email success,give id to form(here its id="myForm", then use style to display:none as shown below)

<?php
if (isset($_POST["submit"])) {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Camino Contact Form';
    $to = 'email@example.com';
    $subject = 'Message Contact Form ';
    $body ="From: $name
 E-Mail: $email
 Message:
 $message";

    if (!$errName && !$errEmail && !$errMessage) {
        if (mail ($to, $subject, $body, $from)) {
            $result='<div>Thank You! I will be in touch</div>';
            echo "<style> #myForm{ display:none; } </style>";
        } else {
            $result='<div>Sorry there was an error sending your message.</div>';
            echo "<style> #myForm{ display:block; } </style>";
        }
    }
}
?>


<form role="form" method="post" action="index.php" id="myForm">


</form>