Forum Moderators: coopster
Can anyone suggest a script which I can get hold of to validate :
several text boxes, a couple of drop-downs and a checkbox, before allowing me to enter my data into mySQL.
I've been hunting high and low for one and can't find a decent one anywhere. I dont know how to write my own - I wish I did.
Thanks in advance.
What have you got so far?
All I've got is the form.
I dont know where to start to be honest.
The data needs to go into SQL db afterwards, which I can get it doing at the moment.
Any help or just some suggestion as to how to get started would be great.
session_start();
$field1_min_length = 6;
$_SESSION['error_msg'] = "";
$_SESSION['bad_fields'] = array();
if (isset($_POST['field1']) && (strlen($_POST['field1']) > $field1_min_length))
{
$set_clause .= "field1='" . $_POST['field1'] . "',";
}
else
{
$_SESSION['error_msg'] .= "Problem with Field 1";
$_SESSION['bad_fields']['field1'] = true;
}
.... etc
if ($_SESSION['error_msg'])
{
show form again with error message and bad fields highlighted.
}
else
{
show the confirmation page.
}
you can check eregi() out at php.net and they'll even have an example how to use
As for the checkboxes..if they dont' check any then the array say..if u use 1 array i assume won't even exist so you can just run an if statement on it..
This seems easy enough.
Thats what i used to make sure they were filled in at least 4 chars and non-blank.
Unless you want to check for specific patterns (like that it is all alpha-numeric, underscores or hyphens), you should stick with strlen() since it will be more efficient.
For many fields, you *will* want to check more than just string length, for others you can't if there is great variation in terms of what will be entered.
Tom