Forum Moderators: coopster
You're not exactly "validating" the form, rather you are assigning a value to a parameter when the user did not indicate one. "Validation" means checking to see that all required/desired fields have been filled before the form is processed.
You can do that at the client using Javascript or some client-side process, or you can do it at your server (as you are).
In either case, the point of "validation" is to keep bad or nonexistent data out of your processing routine (database, etc.). If you choose to accept an "invalid" form and then parse and fix the input before processing it, then power to ya! :)
I would agree with StupidScript, do some client checking at least.
dc
// Check for name.
if (isset ($_POST['name'])) {
$name = ($_POST['name']);
}
else {
$name = 'FALSE';
echo 'Please enter as name';
}
$name = trim($name)
Would it be better to use this instead of isset?
// Check for name.
if (empty($_POST['name'])) {
$name = 'FALSE';
echo 'Please enter as name';
}
else {
$name = ($_POST['name']);
}
I'd avoid using only client side as you're leaving yourself open to security exploits.
If you use the trim function it removes white space from before and after a string. If someone has entered a single white space " " then I don't think the empty function will return true, as there is a character in there. Better to trim it, or also to test for the string length of your post variable to make sure someone has entered some data.
I've changed the code a little:
// Check for name.
$name = trim($_POST['name']);
if (empty($name)) {
$name = FALSE;
echo 'Please enter as name';
}