Mic*_*yen 14
您可以告诉表单提交给PHP的self,然后检查$_POST变量以进行表单处理.这种方法非常适合错误检查,因为您可以设置错误,然后使用用户先前提交的任何信息重新加载表单(即他们不会丢失提交).
单击"提交"按钮时,它会将信息发布到同一页面,在顶部运行PHP代码.如果发生错误(基于您的检查),表单将重新加载显示错误的用户以及用户仍在字段中提供的任何信息.如果未发生错误,您将显示确认页面而不是表单.
<?php
//Form submitted
if(isset($_POST['submit'])) {
//Error checking
if(!$_POST['yourname']) {
$error['yourname'] = "<p>Please supply your name.</p>\n";
}
if(!$_POST['address']) {
$error['address'] = "<p>Please supply your address.</p>\n";
}
//No errors, process
if(!is_array($error)) {
//Process your form
//Display confirmation page
echo "<p>Thank you for your submission.</p>\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;
}
}
?>
<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
<?=$error['yourname']?>
<p><label for="yourname">Your Name:</label><input type="text" id="yourname" name="yourname" value="<?=($_POST['yourname'] ? htmlentities($_POST['yourname']) : '')?>" /></p>
<?=$error['address']?>
<p><label for="address">Your Address:</label><input type="text" id="address" name="address" value="<?=($_POST['address'] ? htmlentities($_POST['address']) : '')?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>
Run Code Online (Sandbox Code Playgroud)
最简单的构造是检测$_POST数组是否为空
if(isset($_POST['myVarInTheForm'])) {
// Process the form
}
// do the regular job
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24333 次 |
| 最近记录: |