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
https://forums.phpfreaks.com/topic/222145-checkbox-array-in-form/
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], ...

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.