Doug Molineux
Doug Molineux

Reputation: 12431

Implode error for PHP

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?

Upvotes: 4

Views: 5273

Answers (3)

Macmade
Macmade

Reputation: 53950

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

Upvotes: 0

Pekka
Pekka

Reputation: 449395

$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)

Upvotes: 2

kb.
kb.

Reputation: 2005

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.

Upvotes: 17

Related Questions