I translated a tool from VB to PHP so that we could plug it in as a Drupal module.
There should be another journal entry about the lessons in bitness, thread safety, WAMP administration, and a handful of other hard lessons learned while deploying this tool, but for now we'll focus on a smaller bug dealing with HTML Checkboxes and the way PHP deals with them once the submit button is pushed.
After a struggle with the translated tool's ability to remember and display query results that were filtered by multiple checkboxes, I located the first of two necessary fixes here:
Practically applied, all of my checkboxes had the name,
name="productline". I simply had to adjust the name to "
I didn't spend much time diving into the 'Why', but at face value, I presume this allows PHP to treat the variable as an array.
Once applied, I was able to access each value by running a foreach on the productline array in the $_POST variable.
$allcodes = "";
foreach($_POST['productline'] as $i)
//create a comma separated string of all selected items stored in the productline post
$allcodes .= $i . ",";
*********NOTE - No Brackets at the end of the 'productline'
All was well and good when I printed the $allcodes in the HTML. Passing multiple values was solved.
Unfortunately, this was not the end of my bugs.
The translation used a string check to re-select checkboxes on submit. For some strange reason, the first box selected would not re-check itself, despite the trigger's presence in the $allcodes variable.
Long story short, strpos has an unexpected behavior as displayed in the warning box here:
I was using the compare operator == to check if a database code value was in the $allcodes string.
If the evaluation returned true, I set a variable as " checked='yes' "
If the character I'm searching for is not in the string, the evaluation returns "0" or FALSE. Oddly, if the character I'm searching for is in the very first position in the string, or position 0, the evaluation returns something to the effect of "yes, and it's in position 0", but to the PHP engine, that is the same as "0". Therefore, we get a false negative.
To cure this, I flipped the polarity of my if statement, and used === (comparatively equals, and ONLY equals) in my expression.
$toggleSwitch = strpos($allcodes,$valueToFind);
$isSelected = "";
$isSelected = "checked='yes'";