tags:

views:

203

answers:

2
<input type="checkbox" class='form' name="checkbox_1" />

<input type="checkbox" class='form' name="checkbox_2" />

<input type="checkbox" class='form' name="checkbox_3" />

.........

<input type="checkbox" class='form' name="checkbox_10" />

The from has been submitted using the "POST" method. identify, which of the check-boxes and write their numbers in increasing order. Separated all numbers by spaces(not new lines) and do not use any HTML formatting.

For Eg:

If check-boxes 3, 5 and 10 are checked.

Ouput would be:

3 5 10

+2  A: 

Iterate over the $_POST array and use preg_match() to pull out the number if it starts with "checkbox_":

$checked = array();
foreach ($_POST as $k => $v) {
  if (preg_match('|^checkbox_(\d+)$!', $k, $matches) {
    $checked[] = $matches[1];
  }
}
echo implode(' ', $matches);
cletus
What vsr said seems more appropiate, this way you don't have to search for a field using regexes.
azkotoki
+3  A: 

Change the markup to something like

<input type="checkbox" class='form' value="1" name="checkbox[]" />
<input type="checkbox" class='form' value="2"  name="checkbox[]" />
<input type="checkbox" class='form' value="3"  name="checkbox[]" />

and to get the values submitted, use a simple loop

foreach($_POST['checkbox'] as $checkbox){
    echo $checkbox . ' ';
}
vsr
if (isset($_POST['checkbox']) }
janmoesen