Jump to content


Photo

Checkbox not returning array


  • Please log in to reply
3 replies to this topic

#1 shadowcaster

shadowcaster
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationUK

Posted 17 April 2006 - 12:48 AM

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

Where am I going wrong? Please help.
--Lurking out of sight, Night shall fall and feed my frenzy, Beneath an eerie moon a change comes over me--[br]Check out Symphony X (The oddysey album) if you like heavy metal

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 17 April 2006 - 01:29 AM

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

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

similar stuff for the 'sen' array...

then in your query use .....WHERE `field` $qrystr_0 ....
follow me on twitter @PHPsycho

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 17 April 2006 - 05:05 AM

[!--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:
<?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>';
?>
Remember that only values of the checkboxes that have been submitted are actually returned to your script.

Ken

#4 shadowcaster

shadowcaster
  • Members
  • PipPip
  • Member
  • 22 posts
  • LocationUK

Posted 17 April 2006 - 08:44 PM

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.
--Lurking out of sight, Night shall fall and feed my frenzy, Beneath an eerie moon a change comes over me--[br]Check out Symphony X (The oddysey album) if you like heavy metal




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users