views:

646

answers:

3

i have a form that utilizes checkboxes.

<input type="checkbox" name="check[]" value="notsure"> Not Sure, Please help me determine <br /> <input type="checkbox" name="check[]" value="keyboard"> Keyboard <br /> <input type="checkbox" name="check[]" value="touchscreen"> Touch Screen Monitors <br /> <input type="checkbox" name="check[]" value="scales">Scales <br /> <input type="checkbox" name="check[]" value="wireless">Wireless Devices <br />

And here is the code that process this form in a external php file.

$addequip = implode(', ', $_POST['check']);

I keep getting this error below;


<b>Warning</b>:  implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in <b>.../process.php</b> on line <b>53</b><br />
OK
A: 

the following site seems to be what you need:

http://www.ozzu.com/programming-forum/desperate-gettin-checkbox-values-post-php-t28756.html

John Boker
+1  A: 

is any of your checkboxes ticked? php’s $_POST array will only have checkboxes which have been ticked

to silence your warning use this:

$addequip = implode(', ', empty($_POST['check']) ? array() : $_POST['check'] );
knittl
I would also make sure that your using the POST form method...
null
i m getting the same error even though the check box are checked. I tried the method from http://www.kirupa.com/web/php_contact_form3.htm still no dice
are you sure, you are using post method on the form? default (no attribute) is get
knittl
A: 

Hi I m the original user who posted this question i couldnt login to my account so posting from another account. After couple hours of trying i somehow managed to make it work partially. Below is the modified form html and process code for checkboxes

<input type="checkbox" name="check" value="Touchscreen"> Touchscreen<br>
<input type="checkbox" name="check" value="Keyboard"> Keyboard<br>
<input type="checkbox" name="check" value="Scales"> Scales<br>

I had to remove the [] so it would work. Also below is the entire post method for those would like to see. It works perfectly fine with every other field.

<form id="contact_form" action="process.php" method="POST" onSubmit="return processForm()">

And below is the php code to process checkboxes. For some reason i have to tell script that $_POST['check'] is an array without it would only return array. All other methods suggested returns invalid argument passed error.

$chckbox = array($_POST['check']);
          if(is_array($chckbox))
       {
         foreach($chckbox as $addequip) {
         $chckbox .="$addequip\n";
         }
       }

So this code works but returns only 1 checkbox value that is ticked even no matter how many you ticked.

What happens if you take the onSubmit="return processForm()" out of your form tag? It's possible that javascript is causing the issue.
dnagirl
No it makes no difference for form submission still getting only one value. It is used for captcha class i m using ajax so the page submission doesnt open another page and form is cleared once submission is done.