Jump to content

checkbox array help


MDanz

Recommended Posts

i have a form with multiple checkboxes, with the same name. so i put them in an array like this.

 

<input type='checkbox' name='test[]' value='' />
<input type='checkbox' name='test[]' value='' />
<input type='checkbox' name='test[]' value='' />
<input type='checkbox' name='test[]' value='' />

 

Now when i do print_r($_POST['test']; , it should display an array and if each checkbox has been ticked or not.  it just comes up blank.

 

Any idea?

Link to comment
Share on other sites

The only reason I can't think of it not showing anything is that nothing was checked.  It would probably help more to print_r the entire $_POST array and see what's coming through.

 

Also, put it inside

 tags when you print_r, it's much easier to read.

[code]echo "
" . print_r($_POST) . "
";[/code]

Link to comment
Share on other sites

The only reason I can't think of it not showing anything is that nothing was checked.  It would probably help more to print_r the entire $_POST array and see what's coming through.

 

Also, put it inside <pre> tags when you print_r, it's much easier to read.

echo "<pre>" . print_r($_POST) . "</pre>";

ok i did that and this the result

 

Array ( [0] => ) 

 

is it because value is blank?

<input type='checkbox' name='test[]' value='' />

 

 

 

Link to comment
Share on other sites

ok i removed value

<input type='checkbox' name='test[]'  />
<input type='checkbox' name='test[]'  />

 

and it displays

Array ( [0] => on ) 

 

but it should be  Array ( [0] => off [1] => on )

Link to comment
Share on other sites

ok i realised what is wrong... if i didn't click on the checkbox it isn't added to the array.

 

<?php 

if(isset($_POST['submit'])) {

print_r($_POST['tags']); 

} else {
?>


<form action="test16.php" method="post">
<input type="checkbox" name="tags[]" value="1" />
<input type="checkbox" name="tags[]" value="2" />
<input type="checkbox" name="tags[]" value="3" />
<input type="checkbox" name="tags[]" value="4" />
<input type="submit" name="submit" value="submit" />
</form>

<?php   }  ?>

 

How do i change the code above so it's four values in the array, indicating what i have clicked on and what i haven't.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.