How can I use multiple form inputs with the same name?

I have several flags (I do not know their number) that are created from the loop in the form.

<form> <input type="checkbox" name="id" value="id"> <input type="checkbox" name="id" value="id"> ...//create in a loop <input type="checkbox" name="id" value="id"> </form> 

My question is, how can I read them if I use <?php $_REQUEST['id']; ?> <?php $_REQUEST['id']; ?> , it only reads the last flag.

+4
source share
2 answers

Use input array:

 <input type="checkbox" name="id[]" value="id_a"> <input type="checkbox" name="id[]" value="id_b"> <input type="checkbox" name="id[]" value="id_c"> <!-- ^^ this makes it an array --> 

$_REQUEST['id'] can be accessed:

 foreach($_REQUEST['id'] as $id) { echo $id; } 

Outputs

id_a
id_b
id_c

Side note: this works with $_POST and $_GET (and not just $_REQUEST ). Generally speaking, although $_REQUEST should be avoided if possible.

+13
source

Use a unique identifier for your flags, for example,

 <form> <input type="checkbox" name="id1" value="value1"> <input type="checkbox" name="id2" value="value2"> ...//create in a loop <input type="checkbox" name="id3" value="value3"> </form> 
-4
source

All Articles