Page 1 of 1

PHP name validation working in reverse

Posted: Tue Feb 26, 2008 6:26 am
by jovankau
Hi,

I have modified some PHP code to validate name entry in a web form. It is working, just opposite to how it should i.e. allow blank entries and doesn't allow entries with names.

Any suggestions on how to resolve this would be greatly appreciated.

Here is is:

Code: Select all

 
 
  // Validate the name
  
  if (isset($_REQUEST['name']) && !empty($_REQUEST['name'])) 
 
{
    /* First lets set up a string without spaces to make this all easier to read */
    $name = trim($_REQUEST['name']);
 
      // the user's name cannot be a null string
      $errors[] = "You must supply a name.";
 
    if (strlen($formVars['name']) > 30) {
      $errors[] =
        "The name can be no longer than 30 characters";
 
}}
 
Thanks,
Jo

Re: PHP name validation working in reverse

Posted: Tue Feb 26, 2008 11:16 am
by Christopher
You should setup a test script to play with PHP's if() statement. Try out the various logic combinations to see how it works.

Code: Select all

// $myvar = '';
// $myvar = 0;
// $myvar = 'foo';
if (isset($myvar) && !empty($myvar)) { echo "isset && !empty"; )
if (isset($myvar) || empty($myvar)) { echo "isset || empty"; )
if (!isset($myvar) && !empty($myvar)) { echo "!isset && empty"; )
if (!isset($myvar) || !empty($myvar)) { echo "!isset || !empty"; )
// etc...
 

Re: PHP name validation working in reverse

Posted: Tue Feb 26, 2008 8:58 pm
by jovankau
Hi Chris,

Have given you suggestions a go. Now I am getting the "you must supply a name" error when a name has been and when it hasn't!

Here is one example of the code:

Code: Select all

 
 if (isset($_REQUEST['name']) && !empty($_REQUEST['name'])) echo "isset && empty";
  
 
{
    /* First lets set up a string without spaces to make this all easier to read */
    $name = trim($_REQUEST['name']);
 
      // the user's name cannot be a null string
      $errors[] = "You must supply a name.";
 
    if (strlen($formVars['name']) > 20) {
      $errors[] =
        "The city can be no longer than 20 characters";
 
}}
 
Do you have any other suggestions?

Thanks,
Jo

Re: PHP name validation working in reverse

Posted: Tue Feb 26, 2008 9:12 pm
by superdezign
The braces aren't connected to the if statement. They are just stray braces. The if statement's block is ended by the echo statement.

Re: PHP name validation working in reverse

Posted: Tue Feb 26, 2008 10:29 pm
by jovankau
Thanks for your help. It has done the trick!

Sorry to ask such basic questions, not even classified as a 'newbie' yet!!

Have a great day,
Jo