PHP Isset question

I want a php verification only if a value is submitted in a field and if so to make sure it has less than 3 characters. This what I have which checks if it has less than 3 characters but not sure how to only trigger if a value has been submitted.

Thanks

if (( strlen($_POST['office']) < 3 ) ){
    $fielderror .="\"Office\" field cannot contain less than three characters<br>";
    $doNotSubmit = true;
  }

Open in new window

Genesis5150Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
This would require two separate tests.  First, to see if the value in $_POST is not empty(), then to test the strlen() of the value.  You might want to test for the maximum strlen() value, too.  All of this stuff we do to filter our external data is very important to the safety and security of your data model.
ChloesDadCommented:
One way would be to check if the length is also > 0

if (( strlen($_POST['office']) < 3 ) AND ( strlen($_POST['office']) > 0) ){

Open in new window

Genesis5150Author Commented:
I tried using this to run two test but it didn't work:
if (!empty($_POST['office']))
	{
		$doNotSubmit = true;
	}
	else if(isset($_POST['office']) && strlen($_POST['office']) < 3)
	{
		$fielderror .= '"Office" field cannot contain less than three characters<br>';
		$doNotSubmit = true;
	}

Open in new window

C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Ray PaseurCommented:
Maybe something along these lines:
<?php // demo/temp_genesis5150.php

/**
 * http://www.experts-exchange.com/questions/28694767/PHP-Isset-question.html
 */
error_reporting(E_ALL);


// INITIALIZE THE VARIABLES WITH OUR ASSUMPTIONS
$fielderror  = NULL;
$doNotSubmit = FALSE;

// IF THE FORM FIELD HAS NOT BEEN POSTED
if (empty($_POST['office']))
{
    $fielderror .="\"Office\" field is required<br>";
    $doNotSubmit = true;
}

// IF THE FORM FIELD IS TOO SHORT
elseif (strlen($_POST['office']) < 3 )
{
    $fielderror .="\"Office\" field cannot contain less than three characters<br>";
    $doNotSubmit = true;
}

// SHOW THE VARIABLE SETTINGS AFTER THE TESTS FOR EMPTY AND STRLEN
var_dump($doNotSubmit, $fielderror);


// END OF THE ACTION SCRIPT, CREATE THE FORM USING PHP HEREDOC NOTATION
$form = <<<ENDFORM
<form method="post">
Office: <input name="office" />
<input type="submit" value="Go!" />
</form>
ENDFORM;

echo $form;

Open in new window

Genesis5150Author Commented:
Actually this worked!
if( $_POST['office'] != "" ){
         if ( strlen($_POST['office']) < 3 ) {
           $fielderror .="\"Office\" field cannot contain less than three characters<br>";
           $doNotSubmit = true;
         }
    }

Open in new window


Thanks everyone.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Just a note... That will only "work" cleanly if your installation of PHP suppresses PHP's reporting of Notice messages.  Otherwise an omitted input control (a blank input that the client did not fill in) will throw a Notice message in the action script.  If you're new to PHP and want to get a foundation in how it all works, this article can help lead you to some good and dependable learning resources.  It takes a while to learn all of this stuff, but it's worth it if you've got any economic interest in the success of your PHP scripts!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
Mark BradyPrincipal Data EngineerCommented:
I know there is a close pending on this question but I would like to add array_key_exists(). I always check to see if a key has been sent in before trying to reference it.
if (array_key_exists('the_key_you_are_looking_for', $_POST) {
    // the key is there so you can do your other checks here
} 

Open in new window

Genesis5150Author Commented:
The field is empty but if it does have a value and the value is less than 3 characters it is FALSE

if( $_POST['office'] != "" ){
         if ( strlen($_POST['office']) < 3 ) {
           $fielderror .="\"Office\" field cannot contain less than three characters<br>";
           $doNotSubmit = true;
         }
    }
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.