tags:

views:

76

answers:

6

I have several like this in my HTML code:

<input class="table" type="checkbox" name="interest[]" value="finger food" />

and this in my PHP code:

$checkboxes = stripslashes($_POST['interest']);

//process the checkboxes
foreach ($checkboxes as $value) {
    $selectedChkbx .= $value . ", ";
}

I am getting:

Warning: Invalid argument supplied for foreach()foreach()

and my $selectedChkbx variable isn't getting any values inside of it. Does anyone know what I'm doing wrong?

A: 

Hi,

stripslashes($_POST['interest']);

stripslashes doesn't work on arrays!

Do it like this:

$checkboxes = $_POST['interest'];

//process the checkboxes
foreach ($checkboxes as $value) {
    $selectedChkbx .= stripslashes($value) . ", ";
}

Answer to your comment:

$array = array('zero', array('one', array('two', 'three', 'four'), 'five'), 'six', 'seven', array('eight'));

echo r_implode(", ", stripslashes_deep($array));

# code from http://ch.php.net/manual/en/function.stripslashes.php
function stripslashes_deep($value)
{
    $value = is_array($value) ?
                array_map('stripslashes_deep', $value) :
                stripslashes($value);

    return $value;
}


# code from http://php.net/manual/en/function.implode.php
function r_implode( $glue, $pieces )
{
  foreach( $pieces as $r_pieces )
  {
    if( is_array( $r_pieces ) )
    {
      $retVal[] = r_implode( $glue, $r_pieces );
    }
    else
    {
      $retVal[] = $r_pieces;
    }
  }
  return implode( $glue, $retVal );
} 

This gives you:

zero, one, two, three, four, five, six, seven, eight

Edit Replaced personal recursive function with a more elegant one ;)

sled
what if the array is nested into multiple levels and $value is again an array?
kali
I've edited my answer ;)
sled
useless code. you all guys are far away from the real life.
Col. Shrapnel
useful or not, it solves the problem he's having. I've replaced my own code with a more elegant one ;)
sled
it solves the problem wrong way. but if your only concern is reputation points, it explains
Col. Shrapnel
actually it solves kali's proplem, not the opening poster's one. And I bet he hasn't such a problem in real, but just for chatter. You did your job in vain, dude
Col. Shrapnel
A: 

you have this: $checkboxes = stripslashes($_POST['interest']);

The stripslashes function will convert the array $_POST['interest'] into an empty string.

Instead you should just:

foreach ($_POST['interest'] as $value) {
    $selectedChkbx .= stripslashes($value) . ", ";
}

Also, magic quotes are deprecated. It is recommend you to turn it off.

thephpdeveloper
+2  A: 

just get rid of this useless stripslashes function

I'd make it with just one line:

$selectedChkbx = implode(", ",$_POST['interest']);
Col. Shrapnel
A: 

You convert it to a string using this:

$checkboxes = stripslashes($_POST['interest']);

My bet is it assigns the value called "Array" as the string. If you want to remove slashes from the entire array use array_filter(). But it would be wiser to just disable magic_quotes on the server so you do not have to strip_slashes.

$checkboxes = array_filter($_POST['interest'], 'stripslashes');

I would highly suggestion you look into the magic quotes issue and fix the problem at the core.

Brad F Jacobs
+3  A: 

stripslashes is probably turning the array into a string. That means $checkboxes is a string and you cannot use a string in foreach.

Either apply stripslashes on each value of the array inside foreach:

foreach ($_POST['interest'] as $value) {
    $selectedChkbx .= stripslashes($value) . ", ";
}

Or use array_map to apply a function on each value of an array:

$checkboxes = array_map('stripslashes', $_POST['interest']);

Then you can join the values with implode:

$selectedChkbx = implode(',', $checkboxes);

And if your stripslashes code is to revert the effects of Magic Quotes, better try to disable them.

Gumbo
That's perfect explanation which I often lack of :)
Col. Shrapnel
A: 

Stripslashes will cast the return value to string. Still people answering here does not have the luxury to "dude just get rid of stripslashes" or "go disable magic quotes!!"

if you or your friend needs to stripslashes and for some reason using magic quotes on (may be a shared host) than you should wrap stripslashes function for arrays.

for example something like:

function myStripper($value) {
    if(is_array($value)) {
        foreach($value as $newValue) {
            return myStripper($newValue);
        }
    } else {
        return stripslashes($value);
    }
}

$checkboxes = myStripper($_POST['interest']);

//process the checkboxes
foreach ($checkboxes as $value) {
    $selectedChkbx .= $value . ", ";
}

should do the trick and recursively strip your variables.

http://uk3.php.net/manual/en/function.stripslashes.php

kali
your function won't work properly.
Col. Shrapnel