Robert
Robert

Reputation: 130

PHP isset does not seem to read correctly in processing

I'm having a weird thing occur in a PHP script I'm writing. I have a rather large form with a lot of fields, checkboxes, radio buttons, etc.

In the processing script there is a lot of repetitious code where I check to see if a field isset and if it is then it outputs a sentence based on that field.

The code I use to process the field is:

if(isset($_POST['testresults'])) { // test results Info
    $testresults = $_POST['testresults'];
    echo $clientInitials . " test results are " . $testresults . ". ";
} 

Now, for some reason, of the 40+ blocks that are identical to this (only with different variables and sentence output) there are two that execute even when the field is empty/not set. As a test, I copied the same block of code to a new PHP file and it has the same behavior, but this is an exact copy of a block that works properly except for the field/variable name. I've cut-n-pasted the field name from the form to make sure there wasn't a spelling mistake or something. As another test, if I change the line to if(!isset($_POST['testresults'])) then it skips that block like it should. So it is reacting as if there is data in that field even when there is not (and it does this with 1 other block as well). The field names/variables are also not used anywhere else in the script and I verified this through doing a search on the file.

The form field is:

<input type="text" name="testresults" value="">

I've tried it with and without the "value" parameter with no change. All the other ones that work correctly have value="" in them.

I've tried clearing my browser cache and there is no difference, still does this odd behavior.

So I guess I'm just wondering if there is a limit to using isset(), or am I missing something?

Upvotes: 1

Views: 119

Answers (1)

Chris Evans
Chris Evans

Reputation: 1265

isset() will only check if a value is set, and not if it is empty, which is why it's more commonly used to check if a form is actually submitted in the first place, or to check $_GET variables. The function you should be using is empty() - or in your case !empty() like so:

if(!empty($_POST['testresults'])) { // test results Info
    $testresults = $_POST['testresults'];
    echo $clientInitials . " test results are " . $testresults . ". ";
} 

Upvotes: 4

Related Questions