您可以告诉表单提交给
PHP的self,然后检查$_POST变量以进行表单处理.这种方法非常适合错误检查,因为您可以设置错误,然后让表单重新加载用户先前提交的任何信息(即他们不会丢失提交).
单击“提交”按钮时,它会将信息发布到同一页面,在顶部运行PHP代码.如果发生错误(基于您的检查),表单将重新加载显示错误的用户以及用户仍在字段中提供的任何信息.如果未发生错误,您将显示确认页面而不是表单.
//Form submitted
if(isset($_POST['submit'])) {
//Error checking
if(!$_POST['yourname']) {
$error['yourname'] = "
Please supply your name.
\n";}
if(!$_POST['address']) {
$error['address'] = "
Please supply your address.
\n";}
//No errors, process
if(!is_array($error)) {
//Process your form
//Display confirmation page
echo "
Thank you for your submission.
\n";//Require or include any page footer you might have
//here as well so the style of your page isn't broken.
//Then exit the script.
exit;
}
}
?>
=$error['yourname']?>
Your Name:
=$error['address']?>
Your Address: