4

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?

3 Answers 3

17

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.