表格验证丢失变量

Let's say I enter a registration form with this ending URL: registration.php?accountType=a The form is parsed on the same page (registration.php). If the user doesn't pass form validation they are taken to registration.php with error messages. So you see I lost my variable accountType=a. My question:

Can I submit a form for validation and retain the accountType variable? I'm aware that this could easily be done using a cookie. I just want a further understanding.

Solution: Although all of the answers below were valid, I went with a Session variable and <form action="registration.php?<?php print 'accountType=' . $_SESSION['accountType']; ?>" etc. Thanks for the help.

You should use this in your form:

<form action="registration.php?accountType=a">

You should use this in your php:

if ($validation_success) {
    header('sucess.php')
} else {
    // don't do anything. The page(registration.php?accountType=a) loads
}

I'm just giving you the idea.

You can easily to it in 3 ways...

  1. Using PHP Sessions. Simply do something like

    session_start(); $_SESSION['namespace'] = array('post' => $_POST, 'get' => $_GET);

  2. Using Cookie (Session seems for me to be more elegant).

  3. Use Ajax request; it will make your tasks easier.

I think you should add the "accountType=$accountType" at the url's end, when you make a http forward, so the user will taken back to the correct page.

I dont see your code, but maybe like this:

if(auth())
{
  //do something...
}
else
{
  header("Location: /registration.php?somerror=true&accountType=" . $accountType);
}