View Full Version : [PHP] Verifying the data that a user has entered
jonathanbull 12-20-2004, 07:18 AM Hi there,
I posted a question on a similar subject to this a while back, and about 7 helpful people pointed out some changes for me to make in my code, which I will soon be doing.
I thought it best that firstly I sort out the main part of this script, some of which I'm finding a bit puzzling!
The following *segment* of coding checks to see if data was entered into specific fields...
if($name == "") {
$nameerror = "You didn't enter your name.";
}
if($surname == "") {
$surnameerror = "You didn't enter your surname.";
}
if($email == "") {
$emailerror = "You didn't enter an email address.";
}
if($username == "") {
$usernameerror = "You didn't enter a username.";
}
if ($password == "") {
$passworderror = "You didn't enter a password.";
include ("registeruser.php");
exit();
}
else {
REST OF CODE
Now, when a password is not entered, there is no problem. It correctly checks the fields, and correctly echos the fields that are missing. The problem begins when you enter something into the 'password' field. It seems to ignore all the other checks and goes ahead and submits the form - even if all the other data is missing!
The problem seems to me that I need to encapsulate the whole of the checks into one 'if' statement. Is this possible, and if so, how would I go about doing this!?
Any help is greatly appreciated! Many thanks in advance,
Jon.
Angelo 12-20-2004, 07:33 AM You did not put an exit statement after the first 4 one, so they are just assigning the error variables and continue to the next conditional. Your Rest code is to execute only if the password is entered not blank.
Try using "or" logical operator. HTML form validation can be another option also without php.
if ($name == "" || $surname == "" || $email == "" || $username == "" || $password == "") {
include ("registeruser.php");
exit();
}
else {
REST OF CODE
}
jonathanbull 12-20-2004, 08:01 AM Thank you very much fcarsenal, that's a huge help.
if ($name == "" || $surname == "" || $email == "" || $username == "" || $password == "") {
include ("registeruser.php");
exit();
}
else {
REST OF CODE
}
How would I define an individual error if the field is missing? For example, in my old code, an individual error was set if a field was missing.
if ($password == "") {
$passworderror = "You didn't enter a password.";
Thanks again for your help, it's much appreciated.
Jon.
Angelo 12-20-2004, 08:19 AM Hmm than you must set them seperately i guess. From a different approach: (i still recommend form validation :) )
if ( $name && $surname && $email && $username && $password ) {
// if all variables set, do the things here and exit after conditional
..
..
..
exit;
}
if($name == "") {
$error = "You didn't enter your name.";
}
if($surname == "") {
$error = "You didn't enter your surname.";
}
if($email == "") {
$error = "You didn't enter an email address.";
}
if($username == "") {
$error = "You didn't enter a username.";
}
if ($password == "") {
$error = "You didn't enter a password.";
}
print $error;
// or you my print a html page here that prints the error
// in a format you want.
?>
jonathanbull 12-20-2004, 11:35 AM Thanks a lot fcarsenal.
I changed the &&'s to || and the code now works fine. Was I right to do this!?
Thanks again!
Angelo 12-20-2004, 02:20 PM No.
&& = AND means statement is true if only both are true
|| = OR means statement is true if any of are true
Xenatino 12-20-2004, 02:27 PM // Set $printError to NULL to avoid confusion later
$printError = NULL;
// Check each variable in turn and assign each error a space in the $errorMessage array
if ($name == "") {
$errorMessage[] = "You didn't enter your name.";
}
if ($surname == "") {
$errorMessage[] = "You didn't enter your surname.";
}
if ($email == "") {
$errorMessage[] = "You didn't enter an email address.";
}
if ($username == "") {
$errorMessage[] = "You didn't enter a username.";
}
if ($password == "") {
$errorMessage[] = "You didn't enter a password.";
}
// If $errorMessage[0] exists, it means that an error has occured
if ($errorMessage[0])
{
// Loop error messages
for($i = 0; $i < sizeof($errorMessage); $i++)
{
$printError .= $errorMessage[$i] . "<br />";
// Assign all the error messages to one variable seperated by line breaks
}
}
// If there are any errors in the $printError variable, display them...
if ($printError)
{
include("registeruser.php");
} else
{
REST OF CODE....
}
Try that....:cool:
jonathanbull 12-20-2004, 02:41 PM [QUOTENo.
&& = AND means statement is true if only both are true
|| = OR means statement is true if any of are true[/QUOTE]
But surely I want to use || ? After all, I still want to echo an error if just one of the fields is missing. Using the &&, won't every single field need to be missing to generate an error!?
Thanks a lot Xenatino. I really appreciate that and I have made all the changes to the code :cool:
Angelo 12-20-2004, 04:30 PM If all variables set it will process the first conditional. If any of the variables are not set ( A and B and C and D and E) it will return false and continue to next conditionals.
hiryuu 12-20-2004, 05:58 PM Xenatino: Nice, although I don't see a need to break building and printing $printError into separate blocks.
// Clear the $errorMessage queue
$errorMessage = array();
// Check each variable in turn and assign each error a space in the $errorMessage array
if ($name == "") {
$errorMessage[] = "You didn't enter your name.";
}
if ($surname == "") {
$errorMessage[] = "You didn't enter your surname.";
}
if ($email == "") {
$errorMessage[] = "You didn't enter an email address.";
}
if ($username == "") {
$errorMessage[] = "You didn't enter a username.";
}
if ($password == "") {
$errorMessage[] = "You didn't enter a password.";
}
// If $errorMessage is non-empty, it means that an error has occured
if (!empty($errorMessage))
{
$printError = join("<br />", $errorMessage);
include("registeruser.php");
} else
{
REST OF CODE....
}
Burhan 12-21-2004, 02:16 AM Just to point out :
if (!empty($errorMessage))
{
$printError = join("<br />", $errorMessage);
include("registeruser.php");
That won't print anything. You probably want
if (is_array($errorMessage))
{
echo implode("<br />",$errorMessage);
require_once 'registeruser.php');
}
hiryuu 12-21-2004, 06:42 PM Since the original example did not echo anything, my assumption is that registeruser.php is used to dress the errors up a bit (and probably regenerate the form). require instead of include is a wise idea, though.
ilyash 12-21-2004, 09:38 PM try it like this..
$error = "You didn't enter your name.";
$error += "<br>You didn't enter ____.";
[dont know if you can do that in php]
xelav 12-23-2004, 07:47 PM I don't like any of this
Try to find good validators
|