0

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?

5
  • 2
    try printing $data[14] to be sure that is an array! Commented Nov 22, 2013 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']. Commented Nov 22, 2013 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? Commented Nov 22, 2013 at 21:04
  • Just check if $_POST['b'] is set, then at least one checkbox is checked. Commented Nov 22, 2013 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! Commented Nov 22, 2013 at 21:21

3 Answers 3

2

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.

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

Comments

0

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

10 Comments

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 could you explain about the modified http headers and the checkboxes? I'm not sure I get you.
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.
Wow, that is a truly awful solution. Why not just use the POST array?
@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.
|
0

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.

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.