homepage Welcome to WebmasterWorld Guest from 54.235.227.60
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
getting checkbox arrays from a form
surrealillusions




msg:4449838
 2:41 pm on May 5, 2012 (gmt 0)

I'm having some trouble understanding why this isn't working, when in theory, it should be.

I have some checkboxes on a form, and they're all related, user picks however many they want.

For example:

<input type="checkbox" name="check_list[]" alt="Checkbox" value="one">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="two">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="three">

In my PHP, I have tried numerous things, but it turns out the basic array that should be sent with the form is empty, when it should have some values in.

<?php
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
}
}
?>

Nothing is displayed.
I've tried

print_r($check_list);

which again, produces nothing. There are no errors (even with error reporting set to E_ALL) produced.

What's going wrong? Have I missed something? I've tried populating the [] with the values, but according to what I've researched, I shouldn't be doing that.

 

incrediBILL




msg:4449853
 4:23 pm on May 5, 2012 (gmt 0)

Not sure exactly what you're doing as you didn't include an entire sample, but the following works:

<form action="" method="post">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="one">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="two">
<input type="checkbox" name="check_list[]" alt="Checkbox" value="three">
<button type="submit">submit</button>
</form>

<?php

echo "Check box test<pre>" ;
print_r($check_list);
echo "</pre>";
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
echo "check=$check<br>";
}
}
?>

Output displayed:
Check box test

Array
(
[0] => one
[1] => two
[2] => three
)

check=one
check=two
check=three

surrealillusions




msg:4449864
 5:35 pm on May 5, 2012 (gmt 0)

That doesn't work here.

I've tried it on a 'live' server (as opposed to my local one), with reporting on E_ALL, and got a variable does not exist error for this line in the code you gave:

print_r($check_list);

But surely it does exist? The variable has no spaces in it, ive checked and double checked the variable and names are exactly the same (thanks to copy/paste).

I really dont know whats happening here.

incrediBILL




msg:4449969
 4:32 am on May 6, 2012 (gmt 0)

Code below solves the error but a missing variable isn't fatal - and I tried it on a live server.

Again, you didn't show us all your HTML so we haven't seen the complete form.

You did click SUBMIT, assuming you have a submit button, right?

<?php

if(!empty($_POST['check_list'])) {
echo "Check box test<pre>" ;
print_r($check_list);
echo "</pre>";
foreach($_POST['check_list'] as $check) {
echo "check=$check<br>";
}
}
?>

rocknbil




msg:4450401
 4:21 pm on May 7, 2012 (gmt 0)

Umm, I know this may seem obvious but I don't see it answered anywhere . . .

Did you check the boxes before submitting? Boxes won't show up in POST or GET if none of them are checked. It's the nature of how they work, and is a good thing, it allows you to do things like

if (isset($_POST['mysinglecheckbox'])) {
// it's checked
}

On the other hand it can complicate things, for example if you're NOT using an array,

<input type="checkbox" name="one" alt="Checkbox" value="one">
<input type="checkbox" name="two" alt="Checkbox" value="two">
<input type="checkbox" name="three" alt="Checkbox" value="three">

and need one of them checked as required, you have to define the list of values internally in the program,

var $checkNames = array ('one','two','three');

$onechecked=null;

foreach ($checkNames as $checkname) {
if (isset($_POST[$val])) {
echo "$val<br>";
$onechecked=1;
}
}
if (! $onechecked) {
// error here
}

surrealillusions




msg:4450441
 5:40 pm on May 7, 2012 (gmt 0)

Checkboxes checked ,yep, submit button in place - yep. All the usual stuff, as it was passing text fields ok, and checkboxes on their own ok (as in not in an array).

However, I've found the problem. I completely stripped everything back, the form, the PHP handling, bit by bit, to work it out. And found the culprit to be this bit of code:


//create array to temporarily grab variables
$input_arr = array();
//grabs the $_POST variables and adds slashes
foreach ($_POST as $key => $input_arr) {
$_POST[$key] = addslashes($input_arr);
}


Probably a snippet of code I picked up to help in cleansing the form data...seems to do it too well, as it got rid of the date altogether :)

Any ideas as to why that would get rid of checkbox array data?

incrediBILL




msg:4450465
 6:49 pm on May 7, 2012 (gmt 0)

Again you didn't show us enough to go on, we would need to see what the addslashes() function does, but based on what I can see that loop obliterates your $_POST variables, therefore checkbox data goes POOF!

surrealillusions




msg:4450473
 7:24 pm on May 7, 2012 (gmt 0)

As far as I know, that's all it was, and that's all it did, in those 6 lines.

The form itself is a fairly big form, and not that general, possibly might break the forum rules of not showing site specific stuff, so I got rid of anything else, just in case.

Will look into adding the addslashes into the specific fields that need it (text inputs and textareas).

Thanks for your time and replies :)

rocknbil




msg:4451357
 3:52 pm on May 9, 2012 (gmt 0)

Huh? addslashes() [php.net] is internal to PHP.

You can't (or shouldn't) treat every post value as an array. Most values will be scalar. Since addslashes works on strings,

addslashes Quote string with slashes


I'm **guessing** this re-nukes the array back to a scalar which is why it "works" on scalar values and kills your arrays.

You'll need to identify if the $value in your loop is an array, something like


foreach ($_POST as $key => $value) {
if (is_array($value)) {
// reset the temporary array
$input_arr = array();
foreach ($value as $array_val) {
// Build the temporary array of values
array_push($input_arr,addslashes($array_val));
}
// After array is stepped through, NOW replace this key with the array
$_POST[$key] = $input_arr;
}
else { $_POST[$key] = addslashes($value); }
}


is_array() [php.net]

Time for a mini rant that may help you. :-) While this is a good exercise in flexing your form array pects, I've never dug using arrays in forms. I like to "see" what the keys are, and IMO using an array for your checkboxes is just making life tons harder on you, especially if it comes to validating the checkboxes. An example might be "if box A is checked, then box B needs to be checked or some other field becomes required." This is far easier if the checkboxes are named as in the example I posted earlier.

Then you're not messing around with arrays and trying to sort it out at all. You're done two days ago. :-)

incrediBILL




msg:4453734
 1:51 pm on May 15, 2012 (gmt 0)

Huh? addslashes() [php.net] is internal to PHP.


LOL - oh well.

I don't know every PHP function off the top of my head, just the ones I use ;)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved