I'm trying to make a simple form. The user selects from a number of form elements (drop-down menus, etc), clicks send, whereupon the form is validated (ie, checking that certain selections aren't left out), and a page is then created summarising the user's choices, along with a shopping cart "buy now" button.
This was the way I started it on a document "choice.php":
Using drop-down menus, there's a number of name/value pairs, including a default "Select..."
<select name="selectFirstChoice" size="1">
<option selected value="unselected">Select...</option>
<option value="ChoiceA">Choice A</option>
<option value="ChoiceB">Choice B</option>
</select>
<select name="selectSecondChoice" size="1">
<option selected value="unselected">Select...</option>
<option value="ChoiceC">Choice C</option>
<option value="ChoiceD">Choice D</option>
</select>
The form's 'action' is "process.php", which firstly gets the posted variables:
$FirstChoice = $_GET['selectFirstChoice'];
$SecondChoice = $_GET['selectSecondChoice'];
My first question: Should I use POST or GET? I know many sites have all the variables in query strings, but doesn't this mean people could (theoretically at least) mess up the string and cause headaches for the page processing (shop cart etc)?
Anyway - then "process.php" goes on to do some validation (check for unselected options) by comparing against the default "unselected" option of the form:
if (($FirstChoice == 'unselected') || ($SecondChoice == 'unselected'))
{
echo '<br><br>You missed out a selection<br><br>Go <a href="choice.php">back</a>';
}
else
// here comes the page...
{
echo 'Here's the HTML with '.$FirstChoice.' and '.$SecondChoice.'.';
echo '<br>(A a bunch of other stuff...)';
}
(I'm going to put in some js client-side validation here also, but at the moment I'm trying to work out about the general form method, and how I can work server-side validation into it.)
My second question: When a user is returned to "choice.php" by a link on the "process.php" page, all the drop-down menu settings are lost (very annoying!). (It's OK if the browser's 'back' button is used, but not when using a link.) What methods can I use to get over this?
My third question: can I keep all this on one PHP page, which checks if the variables have meaningful contents and then displays the form? Can this include the validation?
I realise the 3 questions are probably inter-related, but I'd be glad of any comments and help!!