总结后隐藏电子邮件表格

时间:2015-07-19 15:44:23

标签: php html forms email

我在联系表单页面(index.php)的HTML顶部嵌入了一个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\n E-Mail: $email\n Message:\n $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>

我想在成功提交后隐藏表单。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

要在提交和电子邮件成功后隐藏表单,请为表单提供id(此处为id =“myForm”,然后使用样式显示:none,如下所示)

<?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\n E-Mail: $email\n Message:\n $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>

答案 1 :(得分:0)

在CSS文件中;

.hide {
    display:none;
}

(如果不确定如何执行此操作,此question应该有帮助)

然后在PHP if块内;

$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>';
    }

最后,在index.php上:

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

现在,您可以使用一点CSS和动态变量来隐藏整个表单。

修改

如果您想完全避免使用外部或内部CSS文件,可以直接将CSS内联应用于html元素,就像这样;

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

另一种方法,如果您不使用样式表或任何其他CSS。