在提交表单后刷新包含不同内容的页面


refreshing a page with different content after form submission

我想有一个php联系人表单,当提交(如果没有错误),将刷新同一页面,并删除联系人表单,并将其替换为一些文本,例如:"感谢您与我们联系"。有没有最好的办法?

你可以这样做:

<?php
    $posted = false;
    if( isset($_POST['submit']) ) {
        /* Process $_POST */
        /* Do your things */
        /* Set a variable hinting if a post has been done */ 
        $posted = true;
    }
?>
<!DOCTYPE html>
<html>
    <body>
    <?php if( $posted ): ?>
        <form method="post">
            <input name="foo" />
            <input name="bar" />
            <input name="car" />
            <input name="submit" type="submit" value="Submit" />
        </form>
    <?php else: ?>
        <h1>Thank you for contacting us!</h1>
    <?php endif; ?>
    </body>
</html>

这样做。

<?php
$showform=true;  //dispaying the form for the first time
if(isset($_POST['submit'])){
       //check for errors
       if($errors){
          $msg="errors";
          $showform=true;  // if found errors, setting error msg and displaying the form
       }else{
          //process the form
          $showform=false;  //if no errors, setting success msg and hiding the form
          $msg="SUccess";
       }
}
if(isset($msg)){
         //display the success/error msg
         echo $msg;
}
if($showform==true){
   //your form code
   <form method="post">
        <input name="foo" />
        <input name="bar" />
        <input name="car" />
        <input name="submit" type="submit" value="Submit" />
    </form>
}

?>