Jump to content

processing multiple checkboxes with the same name


hgmme@wa

Recommended Posts

How would you process a form like this checkbox example form, if multiple checkboxs are selected and all of them have the same name?I experimented (see below) and it doesn't seem to make an array with that name so what do you do?

<form action="test.php" method="get">		<?php//I'll actually end up using the 'post' method.	if ( isset($_GET['test']) ) {		$n = 1;		foreach ( $_GET['test'] as $test ) {			echo "Value " . $n . ":" . $test;			$n++;		}	}	?>	<input type="checkbox" value="value1" name="test" />	<input type="checkbox" value="value2" name="test" />	<input type="checkbox" value="value3" name="test" />	<input type="submit" name="submit" /></form>

Link to comment
Share on other sites

you just have to add a [] at the name of the checkbox, like this:<input type="checkbox" value="value2" name="test[]" />because that way you will get the value of the last checkbox onlyhere is the complete code

<form action="test.php" method="get">		<?php//I'll actually end up using the 'post' method.	if ( isset($_GET['test']) ) {		$n = 1;		foreach ( $_GET['test'] as $test ) {			echo "Value " . $n . ":" . $test."<br>";			$n++;		}	}	?>	<input type="checkbox" value="value1" name="test[]" />	<input type="checkbox" value="value2" name="test[]" />	<input type="checkbox" value="value3" name="test[]" />	<input type="submit" name="submit" /></form>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...