When you click on the "Select All" input, it appears to the user as if (s)he's only clicking on the checkbox, but in actuality, the user is clicking on both the checkbox and the submit button due to the fact that events apply to every child element of the applied element. When this happens, the form is submitted (by the invisible submit button) by a button with a name of
checkAll. When the page reloads, it checks to see if the $_POST['checkAll'] postdata is sent, which it is. Afterwards, it checks to see if any of the checkboxes are checked. If none of them are, the script sets a variable called $checked and gives it a boolean value of true. Scrolling down in the code, using that short-if I showed you in my previous code, if $checked is true, it places
checked="checked" in the html causing the result you see.
(if that doesnt make sense, try reading the following: )
So, responding to your edited post:
When $checked is true (the highlighted), it displays the second highlighted, which is the html to make a checkbox appear and valued as checked:
Code:
<input type="checkbox" value="item5" name="items[]" <?php echo isset($checked) ? 'checked="checked"' : '' ; ?> />
And it repeats this for every checkbox that needs to be checked when $checked is true.
But remember: This only happens when there is postdata. If you want to see this in action to understand it better using getdata, change the following highlighted to "get" instead of "post":
Code:
<form method="post" action="">
And in the php change the highlighted to _GET (_POST to _GET):
Code:
<?php
if (isset($_POST['checkAll'])) {
if (empty($_POST['items']))
$checked = true;
}
?>
This will allow you to view the page source and understand where the php is setting the checkbox to checked. In fact, once you've changed the code to get, you can just visit the page like this: page.php?checkAll, and you should be able to actually see in the code what I'm talking about.
The difference between GET and POST data is that get is sent in the URL while post is sent in the headers. For example, the page I'm on right now says
"http://www.codingforums.com/showthread.php?t=265185&page=2", and I can easily change the variables in the URL getting me a different page. However, with post, the variables being sent are not visible to the operator because they are in the headers of the page. You can read about http headers here:
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields (just a little real life example: if you've ever filled out a form on a website then pressed the BACK button, you may notice some funky message about resending the post data. this is because when you go back using post data, the variables you send are handled behind the scenes where when you press the BACK button, its unclear to the browser if you want to keep those variables or dump them as opposed to get where the variables are in the url and the browser doesnt need to worry about them as much)