PJ wrote:
Nathan Rixham wrote:
Chris wrote:
PJ wrote:
And again, this works:
if (strlen($_POST["first_nameIN"]) == 0 ) {
            $obligatoryFieldNotPresent = 1; ...

this does not:

if (strlen($_POST["first_nameIN"]) > 0 ) &&
(strlen($_POST["last_nameIN"]) > 0 ) { echo $first_nameIN, " ",
$last_nameIN);
else (echo "error";)}

But, $first_nameIn and $last_nameIN do echo their contents without the
if clause and we see that the first if clause does work... what am I
doing wrong, again?
Firstly please learn to indent your code (I don't care if this is
just an example of your code, make it easier for everyone who's
trying to help you). It's much easier to read this:

if (strlen($_POST["first_nameIN"]) > 0 ) &&
   (strlen($_POST["last_nameIN"]) > 0 ) {
      echo $first_nameIN, " ", $last_nameIN;
} else {
  echo "error";
}

If you want help we're not going to spend a long time reformatting
your code to try and understand it.

Now to the problem.

Are both first_nameIN AND last_nameIN longer than 0 chars?

var_dump($_POST['first_nameIN']);
var_dump($_POST['last_nameIN']);

maybe you only filled in first_name or last_name but not both.

syntax.. your brackets are all over the place

your wrote:

if (strlen($_POST["first_nameIN"]) > 0 ) &&
   (strlen($_POST["last_nameIN"]) > 0 ) {
      echo $first_nameIN, " ", $last_nameIN;
} else {
  echo "error";
}

should be:

if( (strlen($_POST["first_nameIN"]) > 0) &&
(strlen($_POST["last_nameIN"]) > 0) ) {
    echo $first_nameIN, " ", $last_nameIN;
} else {
  echo "error";
}

weirdly spaced but easier to read version *throws coding standards out
the window*:

if(
   (strlen($_POST["first_nameIN"]) > 0)
  &&
   (strlen($_POST["last_nameIN"]) > 0)
  )
{
    echo $first_nameIN, " ", $last_nameIN;
}
else
{
    echo "error";
}

regards

Oooops, so it was only the parentheses... =-O
Thanks, it works now. I do find it confusing just when and where to put
the brackets...


when in doubt just simplify your code :)

example:


if( (a > 0) && (b > 0) ) {

} else {

}

then replace a and b with your strlen($_POST["XX"])

and hopefully to pre-answer the next question.. :p you should really be checking if the variables are set first, as if somebody requests the page without sending a form you're going to get a tonne of errors..

I'll take 10 out and explain all of this in full for you :)

if(
    isset($_POST['first_nameIN'])
    &&
    ( strlen(trim($_POST['first_nameIN'])) > 0 )
  ) {

REF-A: what we're checking here (as order in which they appear above is:
 - if the form field 'first_nameIN' has been sent in by POST or not
 - AND if the length of the trimmed input is greater than zero
   (trimming it to ensure we don't just have a string like "     "

now as you'll know php provides variables and functions, specifically we want to use functions to bracket off code we often repeat, so you could put the above in a function, the variable being the POST field you want:

function checkPostField( $fieldname )
{
if( isset($_POST[$fieldname]) && ( strlen(trim($_POST[$fieldname])) > 0 ) ) {
      return true;
  }
  return false;
}

this will return true if everything we mentioned in REF-A above is true, and false otherwise; we can then use this function in out if statements to make it more readable and save repeating code:

if( checkPostField('first_nameIN') && checkPostField('last_nameIN') ) {
  echo $_POST['first_nameIN'] . " " . $_POST['last_nameIN'];
} else {
  echo "error";
}

you'll note above that I swapped your $first_nameIN with $_POST['first_nameIN'] - this is because at this point we have never created a variable called $first_nameIN containing $_POST['first_nameIN'] so we'll get an empty error.. we could introduce this in to the code though so it can be used later, as such:

if( checkPostField('first_nameIN') && checkPostField('last_nameIN') ) {
  $first_nameIN = $_POST['first_nameIN'];
  $last_nameIN = $_POST['last_nameIN'];
  echo $first_nameIN . " " . $last_nameIN;
} else {
  echo "error";
}

finally, without addressing anything else that may confuse matters, the only change I made was changing ,(comma) with .(period) - period is the default concatenation operator in PHP, whereas comma is the list operator - both will work in this scenario but it's best to keep good practise and stick to what always works; feel free to skip this next bit, it's purely for those people who will argue with point..

consider:
function echoIt( $string , $lowercase=false , $trim=true ) {
...

a simple function with 3 params.. now let's use the comma as a concatenation device and call it:

echoIt( $var0 , ' ' , $var1 , true, false )
big problems... you just passed in 5 arguments

whereas:
echoIt( $var0 . ' ' . $var1 , true, false )
only 3 arguments as we intended - no problems

and finally PJ, here's the full code from above to make life a little easier - I'm by no means suggesting you use it, but if you want to give it a go and see if your results are correct then feel free.

<?php

function checkPostField( $fieldname )
{
if( isset($_POST[$fieldname]) && ( strlen(trim($_POST[$fieldname])) > 0 ) ) {
      return true;
  }
  return false;
}

if( checkPostField('first_nameIN') && checkPostField('last_nameIN') ) {
  $first_nameIN = $_POST['first_nameIN'];
  $last_nameIN = $_POST['last_nameIN'];
  echo $first_nameIN . " " . $last_nameIN;
} else {
  echo "error";
}

?>

many regards.

nathan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to