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 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.