Why is checkbox not showing as checked?

I have the following piece of code that is used to set up checkboxes during initial page load:

for ($y = 0; $y < $itemcount; $y++) {
           $yesno = (bool) $items[$y];
           $checked = ($yesno) ? 'checked="checked" disabled="true" ': '';
           echo '<td align="center">';
           echo "<input type='checkbox' ". $checked  . "  onclick='chkcountfn($y,\"$rowid\") ' />";
           echo'</td>';
 }

Basically it loops through a string, $items, which is a string of numerics in the format “nnnnnnnnnnnnn”. This can be variable length which is reflected by $itemcount e.g. , “1020000574302”, and gets the number at each position.

If the number is non-zero then the associated checkbox should be checked and disabled.

However although the relevent checkboxes are disabled they are not checked.

I wondered if it was due to having the onclick event, but taking it out makes no difference.

When I check the browser developer tools inspector the line for one of disabled checkboxes shows:

<input type=“checkbox” checked=“checked” disabled=“true” onclick="chkcountfn(4,“row24”) ">

which I thought should work.

I use a very similar piece of code in another PHP file, and this checks the checkbox ok. However instead of echoing the line it uses HTML with inline PHP:

<?php
            $yesno = (bool) $hospchk;
            $hosp = ($yesno) ? 'checked="checked"' : '';
?>
<input type="checkbox"  <?php echo $hosp; ?> />

Is there a reason why the PHP echo version isn’t working?

Well, there is no NAME in this example. Therefore, nothing will be posted for this input field.
Also, you only have to check if a posted field isset to know if it has a value or not. UNCHECKED input checkboxes are empty and not set, therefore, they do not exist until they are checked. I am assuming you are talking about data after the user posted the page, correct? Is that where you created the $items[] array from? A posted results? And, why cast it into a BOOL variable, then compare it? Just see if it is set, meaning see if it exists.

I realise that there is no name, but that is not what the problem is.
It is when the page loads, and the checkboxes are set up that the checked=“checked” is being ignored.whilst the disabled phrase is working

As stated elsewhere in the program suite it works, the only difference being that in that case the input checkbox is not part of a PHP echo statement.

Again, what is inside $items array? Have you dumped it to see if the inputs to your compare are even there? Maybe the data in the $items array is badly formed or empty…

The snippet of code you posted works for me.

Some possibilities -

  1. Where the output is at on the web page is after some broken html, and the browser can only make sense of some of the markup. You should validate the resulting web page at validator.w3.org
  2. Some of the characters in your .php file, e.g. the quotes, are not straight ascii characters and are not understood by the browser. This typically results from copy/pasting code found on the web, i.e. you will notice that quotes you post on this forum, not inside of code blocks are displayed as smart/curly quotes and don’t have any special meaning in both php and html. Delete and retype any suspect characters.

BTW - you only need the checked and disabled attributes. If they are present in the markup, they are true.

No it is all ok - In testing I have it in the with the id which is the second paremeter passed to the onclick event, and I can see everything is ok.

However the real point is why is the checked part of the variable being ignored, whilst the disabled part isn’t.

If you look at the line that is passed to HTML according to the browser developer tools (in bold in the original post) it looks as though it should work.

ErnieAlex: The $items is a string that is initially in the format “0000000000” (can be any number of zeros between 5 and 10, and it is the length of the string that determines $itemcount) and, if changed by the checkboxes will be saved with some of the zeros replaced by a positive integer, usually one, but might be two. This is done by the function called on the onclick event which changes the string in a hidden input. By “unhiding” it I can see that it is working ok, and on saving the record I can see that the string is saved correctly in the table.

phdr: I have passed the code (from the browser view source) into validator, and the only things it didn’t like were the align=“center” statements.I have also checked the file using an ASCII evaluator and it reports all characters are valid ASCII.

What is really confusing is that the system usees identical code elsewhere (for attendance recording) and it works perfectly there.

Align=center is HTML code that is not used these days. Usually style=‘text-align: center;’ is used in the CSS code nowadays.

And, the data is a string you convert to a list of bool’s? Why not just save the array instead?
But, since it works on one section, perhaps you should create a simple test page with the two sections side by side and see what is happening for each. It could be a simple misspelling of something.

Any chance that due to css/styling in affect, you cannot see the check mark in the disabled boxes? With just the code you posted, I get a medium gray filled box, with a lighter gray check mark (chrome browser.) Maybe you are getting a check mark the same color as the box?

Sponsor our Newsletter | Privacy Policy | Terms of Service