Php 表单提交后刷新具有不同内容的页面
我想有一个php联系人表单,提交时(如果没有错误),将刷新同一页,删除联系人表单,并替换为一些文本,例如“感谢您联系我们”。Php 表单提交后刷新具有不同内容的页面,php,Php,我想有一个php联系人表单,提交时(如果没有错误),将刷新同一页,删除联系人表单,并替换为一些文本,例如“感谢您联系我们”。 有没有最好的办法 您始终可以这样做: <?php $posted = false; if( isset($_POST['submit']) ) { /* Process $_POST */ /* Do your things */ /* Set a variable hinting if a pos
有没有最好的办法 您始终可以这样做:
<?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>
}
?>
为提交是否成功设置标志(0或1)。加载页面时,请检查标志并根据其值显示所需内容这是一个非常好的答案,有趣的是,通过使用隐藏的表单字段,您可以创建在一个脚本中运行的多页表单。通过对一致隐藏字段(即“pg”)的值使用开关
或类似开关,您可以选择显示表单的下一部分。很好的答案,一个问题。你有if($posted:)和else:,我从来没有遇到过冒号:之前对于if和else语句,这些冒号可以用来代替花括号{}?@user1925900:Yup。我通常在上述情况下使用:
,在。。。好吧,几乎任何时候都可以。:)