Jump to content

Checkbox not returning array


shadowcaster

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.
Link to comment
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 ....
Link to comment
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
Link to comment
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.
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.