在表单验证和提交后回显其他内容

时间:2014-05-21 14:13:55

标签: php forms validation

如何在验证并提交表单后清除页面内容并显示其他内容?

    <?php 
    if(isset($_POST['Confirm'])) {
    $to = "email@email.com";
        $error = 0;
        $first_name = $_POST['first_name'];
            //Validation things
            if(trim($first_name) == '')     {$error = 1; $first_namerr = 1;}
    $msg ="
    First Name:$first_name
    -------------------------
    ";
    $sub ="Contact";
    $from = "From: Support form";
    @mail($to, $sub, $msg, $from);

//How to clear actual content and echo other ?

    }
    }
    ?>

        <form name="contact" id="contact" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">

            <label for="first_name" class="inner_text"><?php if ($error != 0){ if ($first_namerr == 1) {print("<font style='color: Red;'>");} }?>First Name<?php if ($error != 0){if ($first_namerr == 1) {print("</font>");}} ?></label>
            <input id="first_name" name="first_name" size="30" type="text" value="<? echo $first_name; ?>" /><?php if ($error != 0){ if ($first_namerr == 1) {print('<img src="images/error.gif">');} }?>    

      <input type="submit" id="Confirm" name="Confirm" value="Confirm" />
        </form>

我是PHP的初学者,所以如果你想给出答案,请明确说明!

1 个答案:

答案 0 :(得分:1)

您可以使用if / else语句来控制显示的内容 - 例如:

<?php

if (isset($_POST['Confirm'])) {

    // Your mail code

?>

Thank you, your message has been sent! <!-- This content is shown after form submission -->

<?php } else { ?>

    <!-- Display your email form -->

<?php }; ?>