Jump to content

Archived

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

shadowcaster

Checkbox not returning array

Recommended Posts

Hi. I have a few HTML checkboxes that have the value of the id column of a table in the database -
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
<input type="checkbox" name="del[]" value="1" checked> //value 1 is to represent row 1 in the table
<input type="checkbox" name="del[]" value="2" checked>
<input type="checkbox" name="del[]" value="3" checked>
<input type="checkbox" name="del[]" value="4" checked>
another one of the checkboxes is:
<input type="checkbox" name="seen[]" value="1"> //as with del, the value represents the row id in the table
<input type="checkbox" name="seen[]" value="2">
<input type="checkbox" name="seen[]" value="3">
[/quote]

But when I try to proccess these and get the array of values passed in the 'del' and 'seen' name it doesn't pass the array. Here is the PHP:
[code]
if (isset($_POST['submit'])){
    $delete = addslashes($_POST[del]);
    print_r($delete); //outputs 'Array' only
    $notseen= addslashes($_POST[seen]);
    print_r($notseen); //outputs nothing at all[/code]

Where am I going wrong? Please help.

Share this post


Link to post
Share on other sites
First off you can remove the value= attribute checkboxes can only be true or false so u don't need that.

since they can only have that value you need not use addslashes (unless there is something that a malicious fool would liek to do but difficult to see that happening if you code will only work on true or false!

what are you trying to do with the values? if it is to run in a query you can make a string of the array like so..

[code]<?php
$i = 0;
$qrystr_0 = NULL;
foreach ($_POST['del'] as $key => $value) {
$qrystr_0 .= $i++ == 0 ? "IN (" . $key : ", " $key;
}
$qrystr_0 . = ")";
?>[/code]

similar stuff for the 'sen' array...

then in your query use .....WHERE `field` $qrystr_0 ....

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]First off you can remove the value= attribute checkboxes can only be true or false so u don't need that.[/quote]
That is not true. If a value is given that is what is assigned to a checkbox when checked. Try this test script and see what happens:
[code]<?php
$tmp = array();
$tmp[] = '<form method="post">';
$tmp[] = '<input type="checkbox" name="del[]" value="1" checked>'; //value 1 is to represent row 1 in the table
$tmp[] = '<input type="checkbox" name="del[]" value="2" checked>';
$tmp[] = '<input type="checkbox" name="del[]" value="Three" checked>';
$tmp[] = '<input type="checkbox" name="del[four]" value="4" checked>';
//another one of the checkboxes is:
$tmp[] = '<input type="checkbox" name="seen[]" value="1">'; //as with del, the value represents the row id in the table
$tmp[] = '<input type="checkbox" name="seen[]" value="2">';
$tmp[] = '<input type="checkbox" name="seen[]" value="3">';
$tmp[] = '<input type="submit" value="Test" name="submit">';
$tmp[] = '</form>';
echo implode("<br>\n",$tmp);
if (isset($_POST['submit'])) echo '<pre>' . print_r($_POST,true) . '</pre>';
?>[/code]
Remember that only values of the checkboxes that have been submitted are actually returned to your script.

Ken

Share this post


Link to post
Share on other sites
Cheers guys but I found out what was wrong...

I had to use $_POST[del] for each time I used the array in a process.
If I assigned it to a local array like so: $del = $_POST[del]; then it would NOT make $del into the array with all the data within and it wouldn't work. I'm not sure why this happens but I learnt something new.

Share this post


Link to post
Share on other sites

×

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.