简体   繁体   中英

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. 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'); and using 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.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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