tags:

views:

92

answers:

3

Hi Everyone! I have a form where I've got three checkboxes like this:

    <td>Wireless <input type="checkbox" name="services[]" value="wireless" /></td>
      </tr>
  <tr>
    <td>Cellular <input type="checkbox" name="services[]" value="cellular" /></td>
  </tr>
  <tr>
    <td>Security <input type="checkbox" name="services[]" value="Security" /></td>
<input type="submit" name="submit">

and then I extract($_POST), and have this code

$comServices = implode(",", $services);

but I get an error:

Warning: implode() [function.implode]: Invalid arguments passed in ..

does anyone know why Im getting this error?

+4  A: 

If none of your checkboxes was selected $services would be undefined rather than an empty array.

You can do $comServices = implode(",", (array)$services); to prevent it.

kb
thanks man this worked! Ill accept this answer when the time limit expires
Pete Herbert Penito
You might want to use isset() to prevent warnings.
kb
I'll put an exception where if its empty $comservices = "none" thanks again
Pete Herbert Penito
+1  A: 

$services will be empty when there is no check box checked (empty as in null, not as in "an empty array").

You'd have to test whether $services actually is an array:

if (is_array($services))
 $comServices = implode(",", $services)
Pekka
A: 

Usually, that means your variable is not an array... You can check for it with the is_array() function...

Macmade