Hi, I have a few questions in regards to checkboxes I wanted to confim.

Your code is below:

<input type="checkbox" name="checkbox[]" value="1">
<input type="checkbox" name="checkbox[]" value="2">
<input type="checkbox" name="checkbox[]" value="3">
<input type="checkbox" name="checkbox[]" value="4">
<input type="checkbox" name="checkbox[]" value="5">

Now what happens next?

The checkbox array is checkbox[0] = false ... checkbox[4] = false? (when clicked = true?)
- It seems to only add checkboxes once there clicked

If I wanted to pass only the CLICKED checkboxes what do I do then?

If I wanted to pass the checkboxes value between pages what would be the best method?
- Trying sessions at the moment

Can anyone please confirm my questions and point me into the right direction.

Much appericated, Thankyou, Regards X

8 Years
Discussion Span
Last Post by Shanti C

see this code...it will print only clicked checkboxes..

foreach($_POST['checkbox'] as $key=>$val){
  echo "key: ". $key. " value: ". $val ."<br />\n";
  echo $_POST['checkbox'][0];

and this is for storing in sessions:

foreach($_POST['checkbox'] as $key=>$val){
  echo "key: ". $key. " value: ". $val ."<br />\n";
  //$_SESSION['checked'] = array();
 foreach ($_SESSION['checked'] as $key=>$value) {
     echo 'The value of $_SESSION['."'".$key."'".'] is '."'".$value."'".' <br />';

Votes + Comments
Nice coded comment. Thankyou.

Few questions shant:
- what does session_register() do?
- is my assumption correct that every checkbox checked is added to the array only?

Thanks, Regards X

PS: Ill try putting ur example to practical use in the morning.


session_register() registers the global variable with that name in the current session.

- is my assumption correct that every checkbox checked is added to the array only?

yes...only the checked ones are added to that array..
you have to read more about sessions here


Shanti thanks for your help, the logic from your example worked great!!!

One last remaining question regarding sessions you may be able to answer:
- Declaring your sessions with $_SESSIONS or $_SESSIONS[""] (other words ' ' or " ") is there any difference?

Thanks for your help, Regards X

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.