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.

I have a problem with a particular array in a checkbox input.

$insert .= '<tr>
<td>' .$uname. '</td>
<td>' .$fname. '</td>
<td>' .$lname. '</td>
<td>' .$email. '</td>
<td>'.(($admin == 'y') ? 'Admin':'User').'</td>
<td><input type="checkbox" name="change['.$uid.']" 
           value="'.(($admin == 'y')?'n':'y').'"/>'
                   .(($admin == 'y')?'Make a user':'Make an admin user').'</tr>';

From what i've been reading, this should create an array that can be run through if there are multiple checkbox. The $uid is the user id and the name displays as change[1], change[2] etc.. and this works.

The problem is when i run this through a while statement like so:

while(list($key, $val) = each($_POST['change'])) {

I get this message:

Warning: Variable passed to each() is not an array or object

Does anyone know why this may be happening and how to solve it?

share|improve this question
 
try to print_r your $_POST['change'] to see what it holds. –  yent May 18 '12 at 13:25
add comment

4 Answers

up vote 0 down vote accepted

Your array is not being created when submitted. I took your code and made the required form etc.

var_dump($_POST['change']); returns NULL, even when I remove the $uid.

I wish I could tell you why, but it looks like it 'should' work to me. I suggest cleaning up your code, taking out the ternary operators to something more aesthetically pleasing, then double checking your quotes. PHP hates single quotes, even when they should theoretically work, like in the parameter of your POST array ('change') , it is possible the quotes are confusing it.

Well PHP doesn't hate single quotes, you just have to be careful with them as if they are used incorrectly it means code will not be parsed...

share|improve this answer
 
I think you're right. I've tried the other answers and some have worked but then stopped working again for no apparent reason. I'll have a change around in my code to see if that makes a difference. –  Webmorpher May 18 '12 at 14:08
 
I went through again and found a submit button that i had accidentally set to the same name as the checkbox and that was throwing it all out. –  Webmorpher May 21 '12 at 9:40
add comment

The name of the checkboxes should not contain an index value:

<td><input type="checkbox" name="change[]" value="'.(($admin == 'y')?'n':'y').'"/>'.(($admin == 'y')?'Make a user':'Make an admin user').'</tr>';

share|improve this answer
 
I have tried this but i'm still getting the same result. –  Webmorpher May 18 '12 at 13:30
add comment
//$uid is unique, so you perhaps might use this
// make sure $uid is set correctly
// This is correct for multiple checkboxes

<input type="checkbox" name="change['.$uid.'][]" ... 
share|improve this answer
 
then he has to loop through each array??? –  Venu May 18 '12 at 13:35
 
Yes, at least through each user: foreach ($_POST['value'][$uid] AS $this_user_data) { –  djot May 18 '12 at 13:39
add comment

You need to make the checkbox name = change[]

And the value = $uid

Then you can iterate through $_POST['change'] and retrieve $uid

EDIT Remember that only the checked checkbox values are sent

share|improve this answer
 
So this did get rid of the message but it means i don't have the value that i had before, which was y or n, and i was trying to add into a database. Is there anywhere else i can put those values so i can still use them? –  Webmorpher May 18 '12 at 13:52
 
You need to have the uid of the user - is that right? The checkbox will return the uid if it's ticked and return nothing at all if not ticked. That should give you the information you need to write to the database. –  Pete May 18 '12 at 13:56
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.