Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So I am trying to email the results of a form using PHP. inside my form I have a bunch of checkboxes. The script below works when at least 1 checkbox in a group is checked. If none of the checkboxes are checked I receive the following error:

Warning: Implode() [function.implode]: Invalid augments passed in {name of php doc} on line {xxx} Array

Here is a sample of the code I'm using:

 <?php
 $data = array();
   foreach ($_POST as $key => $value) {
      $data[] = $value;   
   }

 if(!$data[14]) //$data[14] is an array of checkbox values
    {echo 'No User Selection';}
 else
    {echo implode(" | ", $data[14]);} //This is where the error occurs 
 ?>

HTML code

 <label for="b0" class="left">Item 1</label>
 <input type="checkbox" name="b[0]" id="b0" value="Item 1"/>
 <label for="b1" class="left">Item 2</label>
 <input type="checkbox" name="b[1]" id="b1" value="Item 2"/>
 <label for="b2" class="left">Item 3</label>
 <input type="checkbox" name="b[2]" id="b2" value="Item 3"/>
 ect....

Does anyone have an idea why I'm receiving this error?

share|improve this question
2  
try printing $data[14] to be sure that is an array! –  Daniele Brugnara Nov 22 '13 at 20:57
1  
Why are you converting your $_POST array - that has easy identifyable keys - to a new array where you have to guess what the key is? Add a new field before the checkboxes and the whole thing collapses. Just use $_POST['b']. –  jeroen Nov 22 '13 at 21:00
    
I think thats whats happening. When none of the checkboxes are checked $data[14] becomes the next input with a value posted. When at least one checkbox is checked $data[14] is an array?? How can I easily assign all posts to a variable without having to do them individually? –  Austin Nov 22 '13 at 21:04
    
Just check if $_POST['b'] is set, then at least one checkbox is checked. –  jeroen Nov 22 '13 at 21:08
    
As mentioned by others, use the POST array. Looping over and creating the data array in order to get the value by a magic number is just plain madness. There are hundreds of ways in which this will go wrong! –  Rob Baillie Nov 22 '13 at 21:21
add comment

3 Answers

up vote 0 down vote accepted

If the checkbox is not checked, it is not sent to the server. I suggest you to do something like this:

<label for="b0" class="left">Item 1</label>
<input type="hidden" name="b[0]" value=""/>
<input type="checkbox" name="b[0]" id="b0" value="Item 1"/>
<label for="b1" class="left">Item 2</label>
<input type="hidden" name="b[1]" value=""/>
<input type="checkbox" name="b[1]" id="b1" value="Item 2"/>
<label for="b2" class="left">Item 3</label>
<input type="hidden" name="b[2]" value=""/>
<input type="checkbox" name="b[2]" id="b2" value="Item 3"/>

This way, you are sure that b[0], b[1], etc. are always sent

share|improve this answer
    
HTTP headers could be modified when data are sent, so it might be a better idea to check if variables exists on server side. –  evuez Nov 22 '13 at 21:03
    
@evuez could you explain about the modified http headers and the checkboxes? I'm not sure I get you. –  Matthew Nov 22 '13 at 21:09
    
That's not a very nice solution: You need double the amount of input fields and the only difference on the server-side is that now you have to check for empty instead of isset. –  jeroen Nov 22 '13 at 21:14
    
Wow, that is a truly awful solution. Why not just use the POST array? –  Rob Baillie Nov 22 '13 at 21:19
1  
@RobBaillie I totally agree with you, but I only solved his problem, and showed a very useful trick to send checkbox input to the server even if those are unchecked. The author does definitely a terrible mistake by accessing $data[14], if he changes his form, his code might break. But that was already mentioned to him, and I would need to explain some programming basics... I don't understand why the downvotes. –  Matthew Nov 22 '13 at 21:31
show 5 more comments

Make sure the variable a) is set and b) is an array.

$data = array();
foreach ($_POST as $key => $value) {
    $data[] = $value;   
}

if ( !isset($data[14]) || !is_array($data[14]) ) {
     echo 'No User Selection';
} else {
    echo implode(" | ", $data[14]);
}

Always properly check variables using isset(), unless of course you like giant error logs! I also suggest using the $_POST keys as the keys for $data, thus making life even easier when you want to look up a specific $_POST item.

share|improve this answer
add comment

You are causing the problem yourself by making the data array. Just go straight to the POST array. Everything else is overcomplicating the problem.

<?php

     if(!isset ($_POST['b']) || !is_array($_POST['b'])) {
       echo 'No User Selection';
     } else {
       echo implode(" | ", $_POST['b']);
     }

?>

In addition, if the content of your form changes slightly, or you want to reorder the fields then your magic number 14 will no longer work.

Your code will be unbelievably fragile unless you change it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.