简体   繁体   中英

How can get values from select-inputs with the same name using php method POST?

I'm trying to do something like this:

HTML

<form action="" method="post">

 <input type="hidden" name="id[]">
 <select name="type[]">
   <option value="0">Option 0</option>
   <option value="1">Option 1</option>
 </select>

 <input type="hidden" name="id[]">
 <select name="type[]">
   <option value="0">Option 0</option>
   <option value="1">Option 1</option>
 </select>

 <input type="hidden" name="id[]">
 <select name="type[]">
   <option value="0">Option 0</option>
   <option value="1">Option 1</option>
 </select>
</form>

PHP

<?php
 for ($i=0; $i < sizeof($_POST['id']) ; $i++) {
  $id[$i] = $_POST['type'][$i];
  echo $id[$i];
 } 
?>

But all can I get is the first select-input value. How can I get the others?

<form action="#" method="post">

<input type="hidden" name="id[]">
<select name="type[]">
  <option value="0">Option 0</option>
  <option value="1">Option 1</option>
</select>

<input type="hidden" name="id[]">
<select name="type[]">
  <option value="0">Option 0</option>
  <option value="1">Option 1</option>
</select>

<input type="hidden" name="id[]">
<select name="type[]">
  <option value="0">Option 0</option>
  <option value="1">Option 1</option>
</select>

<!-- Add a sumbmit button -->
<input type="submit" value="Go">
</form>

PHP Code :

<?php
  if(!empty($_POST)){

  for ($i=0; $i < count($_POST['id']) ; $i++) {
     $id[$i] = $_POST['type'][$i];
  }
  print_r($id);
  /* 
  Return : Array ( [0] => 0 [1] => 1 [2] => 1 )
  */
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM