Jump to content

PHP Isset question


Genesis5150

Recommended Posts

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;
  }
Link to comment
https://forums.phpfreaks.com/topic/297173-php-isset-question/
Share on other sites

As your title says use isset

if (!isset($_POST['office']))
{
    $fielderror .= 'Please fill in the "Office" field';
    $doNotSubmit = true;
}
else if(isset($_POST['office']) && strlen($_POST['office']) < 3)
{
    $fielderror .= '"Office" field cannot contain less than three characters<br>';
    $doNotSubmit = true;
}
Link to comment
https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515573
Share on other sites

Now that I'm thinking about it I'm trying to use !empty() to first check if it is empty and then do a check if there is a value to make sure the value is not less than 3 characters. This is what I have:
 

if (empty($_POST['office']))
  {
    $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;
  }

But the code above didn't work.

Thanks

Link to comment
https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515575
Share on other sites

Sorry for the misunderstanding. What I'm trying to look for is a code that only works if there a value to that field. If a value has been posted then make sure it is not less than 3 characters. No error messages are displayed if it is empty and only display an error message if the value of that field is less than 3 characters if posted.

Link to comment
https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515582
Share on other sites

all text, textarea, password, hidden, select/option, and usually submit, form fields will be set if the form has been submitted. you would use an isset() statement to detect if a form has been submitted, like in your first thread on this site.

 

note: zeros are considered to be empty(), so, someone entering a '0' will pass the empty() test,  if the string length is what you are actually looking for, either test the value against an empty string '' or test the strlen() of the value.

 

for your stated logic test - empty is okay or if not empty, three or more characters is okay. or conversely/complementary, the error is if not empty and less than three characters -

$len = strlen($_POST['office']); // i'm going to assume that you have trimmed all the submitted data, so that spaces, tabs, and new-lines have been removed before/after any actual value
if($len != 0 && $len < 3){
    // error condition

}

another way to state this error condition would be if the length is greater than zero and less than 3 - if($len > 0 && $len < 3){

 

here's a hint that will simplify your error logic. use an array $errors (to initialize it - $errors = array(); ) and just set elements in the array for each error message ($errors[] ='"Office" field cannot contain less than three characters';). to test if there are any errors, just test if the array is empty(). if the array is empty, there are no errors. if it is not empty, there are errors. to display the error messages, just loop over the array or you can simply implode() it.

Link to comment
https://forums.phpfreaks.com/topic/297173-php-isset-question/#findComment-1515586
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.