使用PHP提交后隐藏联系表单?

时间:2013-12-17 21:22:55

标签: php jquery html forms

我有一个联系表单,我想在某人完成并提交他们的信息后隐藏。下面是代码。理想情况下,我想隐藏HTML字段集标记之间的所有内容。我知道这可以用jQuery完成,但是可以用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\n E-Mail: $email\n Message:\n $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>

更新:在尝试了一些建议之后,我意识到由于页面在提交表单后重新加载,因此尝试隐藏表单毫无意义。你点击提交,表单消失,页面重新加载,表单再次出现。所以我在容器中包围了from并修改了CSS,因此消息显示在表单上方,这很好。谢谢大家的帮助。非常感谢!

3 个答案:

答案 0 :(得分:0)

试试这个:

if(!isset($_POST['submit']))
{
    <form id="contact" method="post" action="index.php">
    <!--Contents in between-->
    </form>
}

同时将您的HTML更改为:

<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>
</form>
    <!-- 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\n E-Mail: $email\n Message:\n $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>...</form>

中包含联系表单详细信息

答案 1 :(得分:0)

成功时,设置一个变量:

$success = true;

然后围绕HTML表单:

<?php echo $success ? '<!--' : ''; ?>
// form here...
<?php echo $success ? '-->' : ''; ?>

答案 2 :(得分:0)

为什么不将php代码放在表单上方并添加if条件?如果您只想为整个浏览器会话显示一次表单,则必须使用php会话来存储标志。

<form id="contact" method="post" action="index.php">
    <fieldset>
        <!-- Contact Form Details -->
        <?php 
        $success = false;
        $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\n E-Mail: $email\n Message:\n $message";

        if ($_POST['submit'] && $human == '6') {
            if (mail($to, $subject, $body, $from)) {
                $success = true;
                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>';
        }

        if ($success) {
        ?>
        <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>
        <?php } ?>
    </fieldset>

</form>