Jump to content

checkbox array in form


smerny

Recommended Posts

Why is it grouping the checked boxes instead of keeping them in order like area is?

if(isset($_POST['submit'])){
for($i=0; $i<10; $i++){
	echo "<br />Ignore = [".$_POST['ignore'][$i]."]";

	if($_POST['ignore'][$i]!="true")
		echo ", Area = ".$_POST['area'][$i];
}
}
else
{
echo "<form method='post'><table>";
for($i=0; $i<10; $i++){
	echo "
	 <tr>
	  <td><input type='checkbox' name='ignore[]' value='true' /></td>
	  <td><input type='text' name='area[]' /></td>
	 </tr>\n";
}
echo "
</table>
<input type='submit' name='submit' value='Submit' />
</form>";
}	

 

Input:

Checked - ignored

Unchecked - a

Checked - ignored2

Unchecked - b

Checked - ignored3

Unchecked - c

Unchecked - d

Checked - ignored4

Unchecked - e

Checked - ignored5

 

Output:

Ignore = [true]

Ignore = [true]

Ignore = [true]

Ignore = [true]

Ignore = [true]

Ignore = [], Area = c

Ignore = [], Area = d

Ignore = [], Area = ignored4

Ignore = [], Area = e

Ignore = [], Area = ignored5

Link to comment
Share on other sites

I recommend you add the following to your code so that you can see what you are actually getting-

 

echo '<pre>',print_r($_POST,true),'</pre>';

 

You only get the checkboxes that are checked, but you get all the text fields. If you want to assoicate the correct checkbox with the same numbered text field you would need to put the $i index counter into the [] array brackets so that ignore[0] corresponds to area[0], ignore[1] corresponds to area[1], ...

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.