简体   繁体   English

如何验证表单然后移至另一页PHP

[英]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: 由于这更多是关于最佳/通用做法的,因此这里是一种决定何时在以下情况下使用哪种方法的方法:

  1. If you have a simple flag or a small data set (say about 5-10 small params), build a query string as mentioned by Brian. 如果您有一个简单的标志或一个小的数据集(例如大约5-10个小参数),请按照Brian提到的那样构建查询字符串。 Search is a good example of this. 搜索就是一个很好的例子。
  2. On the contrary if you need to access a relatively larger data set or confidential information, you could use a combination of query string and code meant to extract information based on the query string parameter. 相反,如果您需要访问相对较大的数据集或机密信息,则可以使用查询字符串和用于根据查询字符串参数提取信息的代码的组合。 For example, passing users' customer ID to retrieve the rest of the information from your database or SESSION or both. 例如,传递用户的客户ID来从数据库或SESSION或两者中检索其余信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM