Log in

View Full Version : Form data!



connor4312
03-16-2011, 06:17 PM
Hi,

I have a list of posts generated from a mysql database with checkboxes whose name is their corresponding post's ID, and using those checkboxes one should be able to delete the posts.

I've tried

print_r($_POST['todelete']);

foreach($_POST['todelete'] as $key => $my_var) {
printf("Key #%d has the value <strong>%s</strong>", $key, $my_var);
}

But that only shows the checks that are on, like:

Key #0 has the value onKey #1 has the value onKey #2 has the value onKey #3 has the value on

So, I'm basically asking how to retrieve the data from all form elements, checkboxes, regardless if they are on or off.

traq
03-16-2011, 09:47 PM
Can you show your form's html markup?

connor4312
03-17-2011, 05:21 PM
<input type='checkbox' name='todelete[]'>

All entries are ordered, so I can match up which ones are being deleted.

traq
03-17-2011, 07:46 PM
okay:

you will never get "off" checkboxes to show up. they're just not supposed to.

two possible solutions:

1) number your checkboxes. I think this is the quickest, most straightforward solution. this way, it's easy to see which item the checkbox belongs to.
<input type="checkbox" name="todelete[1]">
<input type="checkbox" name="todelete[2]">
<!--etc.-->

2) use radios instead. that way, you'll get a "yes" or "no" value for each item. However, you'll need two buttons for each item, and I <i>think</i> you might need to index them anyway; or your yes/no counts might get confused.

<input type="radio" name="todelete[1]" value="yes"> Delete
<input type="radio" name="todelete[1]" value="no"> Keep