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

Recommended Answers

All 5 Replies

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:

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

     }
}
commented: Nice coded comment. Thankyou. +1

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

i think no difference between them....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.