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

时间:2013-01-03 23:23:03

标签: php

我希望有一个php联系表单,在提交时(如果没有错误),将刷新同一页面,并删除联系表单,并将其替换为某些文本,例如“感谢您与我们联系”。 有没有最好的方法来做到这一点?

2 个答案:

答案 0 :(得分:2)

您可以随时执行以下操作:

<?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>

答案 1 :(得分:0)

这样做。

<?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>
}


?>