FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Help With Simple PHP Code Snipit





Dennise
OK, on a form, I'm checking three text-boxes to make sure ALL have (numerical) entries, or NONE have entries. If only one or two boxes have entries, I want to prompt the page viewer that he has made an incomplete data entry. If none of the three have entries, then nothing should happen.

Here's my PHP code.

Code:
IF (isset($csizeqty1) || isset($csizeh1) || isset($csizew1))
{IF (!isset($csizeqty1) || !isset($csizeh1) || !isset($csizew1))
{echo "Custom size 1 input is incomplete";
}}


This does NOT catch incomplete entries for "csize1".

Can someone please help me out?
Fire Boar
You want empty or (in your case) is_numeric, not isset.
jmraker
Code:
if(!empty($_POST['field1']) || !empty($_POST['field2']) || !empty($_POST['field3'])){ // one is not empty
  if(!is_numeric($POST['field1]))
    addError('Field 1 is not numeric');
  if(!is_numeric($POST['field2]))
    addError('Field 2 is not numeric');
  if(!is_numeric($POST['field3]))
    addError('Field 3 is not numeric');
}

if(numErrors() == 0){
  // Form is ok
}
else
  printErrors();


Where 3 functions can keep track of the errors
Dennise
Thanks for the tips. JMRAKER's code looks good, but I don't know how to make those functions.

The following should work, but I get an error message about the "unexpected" else.

Code:
//test if any fields are not empty:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1']))
//if one or more fields are not empty, test that all are numbers:
{if (is_numeric($POST['csqty1'])&& is_numeric($POST['csh1'])&& is_numeric($POST['csw1']))
//if they're all numbers, do nothing
{//OK they are all numbers}
// if they're not all numbers, echo a message
else {echo "Incomplete Input";}}
//resume code here if all fields are empty OR if all fields are numeric



Can someone please point out where I've gone wrong?
Fire Boar
Yes. Your code is messy messy messy. If properly indented, the problem will become clear.

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1']))
{
  //if they're all numbers, do nothing
  if (is_numeric($POST['csqty1']) && is_numeric($POST['csh1']) && is_numeric($POST['csw1']))
  {
    //OK they are all numbers}
    // if they're not all numbers, echo a message
    else
    {
      echo "Incomplete Input";
    }
  }
  //resume code here if all fields are empty OR if all fields are numeric


That is the logical layout of your code. As you can see... your problem is in the comment "//OK they are all numbers}" That } is being counted as part of the comment. The code should actually look like:

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1']))
{
  //if they're all numbers, do nothing
  if (is_numeric($POST['csqty1']) && is_numeric($POST['csh1']) && is_numeric($POST['csw1']))
  {
    //OK they are all numbers
  }
  // if they're not all numbers, echo a message
  else
  {
    echo "Incomplete Input";
  }
}
//resume code here if all fields are empty OR if all fields are numeric


Or if you prefer, here's an alternative format which is a little more compact.

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1'])) {
  //if they're all numbers, do nothing
  if (is_numeric($POST['csqty1']) && is_numeric($POST['csh1']) && is_numeric($POST['csw1'])) {
    //OK they are all numbers
  }
  // if they're not all numbers, echo a message
  else {
    echo "Incomplete Input";
  }
}
//resume code here if all fields are empty OR if all fields are numeric


And we can simplify this further - if you have an empty if loop it would be better to simply invert your logic as follows:

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1'])) {
  // if they're not all numbers, echo a message
  if (!is_numeric($POST['csqty1']) || !is_numeric($POST['csh1']) || !is_numeric($POST['csw1'])) {
    echo "Incomplete Input";
  }
}
//resume code here if all fields are empty OR if all fields are numeric


This amounts to the same thing, but in fewer lines of code.
Dennise
Fire Boar wrote:
Yes. Your code is messy messy messy. If properly indented, the problem will become clear.

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1']))
{
  //if they're all numbers, do nothing
  if (is_numeric($POST['csqty1']) && is_numeric($POST['csh1']) && is_numeric($POST['csw1']))
  {
    //OK they are all numbers}
    // if they're not all numbers, echo a message
    else
    {
      echo "Incomplete Input";
    }
  }
  //resume code here if all fields are empty OR if all fields are numeric


That is the logical layout of your code. As you can see... your problem is in the comment "//OK they are all numbers}" That } is being counted as part of the comment. The code should actually look like:

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1']))
{
  //if they're all numbers, do nothing
  if (is_numeric($POST['csqty1']) && is_numeric($POST['csh1']) && is_numeric($POST['csw1']))
  {
    //OK they are all numbers
  }
  // if they're not all numbers, echo a message
  else
  {
    echo "Incomplete Input";
  }
}
//resume code here if all fields are empty OR if all fields are numeric


Or if you prefer, here's an alternative format which is a little more compact.

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1'])) {
  //if they're all numbers, do nothing
  if (is_numeric($POST['csqty1']) && is_numeric($POST['csh1']) && is_numeric($POST['csw1'])) {
    //OK they are all numbers
  }
  // if they're not all numbers, echo a message
  else {
    echo "Incomplete Input";
  }
}
//resume code here if all fields are empty OR if all fields are numeric


And we can simplify this further - if you have an empty if loop it would be better to simply invert your logic as follows:

Code:
//test if any fields are not empty:
//if one or more fields are not empty, test that all are numbers:
if(!empty($_POST['csqty1']) || !empty($_POST['csh1']) || !empty($_POST['csw1'])) {
  // if they're not all numbers, echo a message
  if (!is_numeric($POST['csqty1']) || !is_numeric($POST['csh1']) || !is_numeric($POST['csw1'])) {
    echo "Incomplete Input";
  }
}
//resume code here if all fields are empty OR if all fields are numeric


This amounts to the same thing, but in fewer lines of code.


Thanks again Fire Boar, I see one of my errors (as you pointed out) and no longer get error messages. I tried all your alternates, but they all have one problem: The "echo "Incomplete Input";" is always executed ...... even if all fields are empty or even if all fields are numeric. This 'statement' should NOT be executed under either of those conditions.

This is so simple but it's driving me nuts! I think the problem lies somewhere in the 'nested' IF's but I just can't see it.

To repeat what I'm after:

If all three fields are empty or if all three fields are numeric, then nothing should happen.

Otherwise, the echo "Incomplete Input"; should be executed.

Help

Sad
Dennise
The problem was with the is_numeric() checks.

The check variables were $POST .................. instead of $_POST ..................

It works OK now!

Thanks so much Very Happy
Fire Boar
Oh snap, how did I miss that? I guess I just wasn't looking for problems with the variable names and they remained wrong in the copy/paste. Good to know it's working now.
Related topics
simple php problem, please help...
Simple PHP Navigation
Show PHP Code
Simple PHP forum code needed!
Help with php code
mySQL Query/PHP code - get the highest value...
Anyone care to help me with a simple PHP Form ?
Need help with PHP script[RESOLVED]
Need Expert help in php-mysql
CSS for making images go through a php code.
What is a good or bad php code?
How to build simple PHP upload form
I need help with a very simple php code :(
need help in a simple php register script based on flat file
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.