[英]How to validate a form then move to another page PHP
I have a form that is to submit a date via 3 drop down boxes. 我有一个通过3个下拉框提交日期的表格。 I currently have the form submitting to itself then checking to make sure the date is valid.
我目前有表单提交给自己,然后检查以确保日期有效。
if(isset($_POST['submit']))
{
$day = $_POST['day'];
$year= $_POST['year'];
$month=$_POST['month'];
$date= $year.'-'.$month.'-'.$day;
//**********************************Validate Date
if (!checkdate($month,$day,$year)){
echo $error= "Invalid Date Please Renter";
}
}
I am curious how I should go about going to the next page with the variables if the date is valid. 我很好奇如果日期有效,我应该如何使用变量转到下一页。 I am thinking
header('Location: nextpage.php');
我在想
header('Location: nextpage.php');
and using Session()
? 并使用
Session()
吗? it workss but I am wondering if that is the CORRECT way. 它有效,但我想知道这是否是正确的方法。
You could also call the header function like this: 您还可以像这样调用标头函数:
header('Location: nextpage.php?year='.$year.'&month='.$month.'&day='.$day);
This way you don't need to worry about the session, and the data will be passed via $_GET parameters. 这样,您不必担心会话,数据将通过$ _GET参数传递。
Why dont you validate the input in another page and if the input is not valid, redirect the user back to the form. 您为什么不验证另一页中的输入,如果输入无效,则将用户重定向回表单。 If the input is valid, they are already on the 'another' page.
如果输入有效,则它们已经在“另一个”页面上。 You can than use $_POST['day'] etc without setting session.
然后可以使用$ _POST ['day']等,而无需设置会话。 I believe this way is safer than storing it inside session.
我相信这种方式比将其存储在会话中更安全。
Since this is more about best/common practices here is a way to decide what method should be used when: 由于这更多是关于最佳/通用做法的,因此这里是一种决定何时在以下情况下使用哪种方法的方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.