如何获取具有相同名称的多个文本输入的值

how to get value of Multiple text inputs with same name

我刚刚创建了一个表单,其中有一些同名的输入字段。我想在提交表单后将所有这些值存储在一个变量中。

密码是

      <div class="selectContainer">
        <label>Number Of Person:</label>
      <select id="ddTest" name="person">
         <option value="">select</option>
        <option value="1">1</option>

        <option value="2">2</option>

        <option value="3">3</option>

        <option value="4">4</option>

        <option value="5">5</option>

        <option value="6">6</option>
         </select>

       </div>
        <div class="hidden is1">
         <a href="#" class="remove" rel="1">remove</a>

    <label>Name</label>
     <input type="text" name="name" >
   <label>Age</label>
    <input type="text" name="age" >

</div>

    <div class="hidden is2">
    <a href="#" class="remove" rel="2">remove</a>
    <label>Name</label>
    <input type="text" name="name" >
    <label>Age</label>
    <input type="text" name="age" >
    </div>

         <div class="hidden is2">
        <a href="#" class="remove" rel="2">remove</a>
    <label>Name</label>
    <input type="text" name="name" >
    <label>Age</label>
    <input type="text" name="age" >
        </div>

         <div class="hidden is3">
         <a href="#" class="remove" rel="3">remove</a>

         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

         <div class="hidden is3">
         <a href="#" class="remove" rel="3">remove</a>
         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

         <div class="hidden is3">
         <a href="#" class="remove" rel="3">remove</a>
         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
          </div>


          <div class="hidden is4">
         <a href="#" class="remove" rel="4">remove</a>

         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
          </div>


         <div class="hidden is4">
         <a href="#" class="remove" rel="4">remove</a>
         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

         <div class="hidden is4">
         <a href="#" class="remove" rel="4">remove</a>
          <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
          </div>

         <div class="hidden is4">
         <a href="#" class="remove" rel="4">remove</a>   
         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
        </div>
        <div class="hidden is5">
         <a href="#" class="remove" rel="5">remove</a>

         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

          <div class="hidden is5">
           <a href="#" class="remove" rel="5">remove</a>
         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

          <div class="hidden is5">
           <a href="#" class="remove" rel="5">remove</a>
          <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

          <div class="hidden is5">
           <a href="#" class="remove" rel="5">remove</a>
          <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
         </div>

          <div class="hidden is5">
           <a href="#" class="remove" rel="5">remove</a>

         <label>Name</label>
         <input type="text" name="name" >
         <label>Age</label>
         <input type="text" name="age" >
        </div>`

如何将这个值存储在一个变量中。?请帮助我是 php 新手

使用 [] 为所有相似的名称创建一个数组:

<input type="text" name="name[]" >
<input type="text" name="age[]" >

然后你可以遍历它们:

for($i = 0; $i <= count($_POST['name']); $i++) {
   echo $_POST['name'][$i] . "<br>";
   echo $_POST['age'][$i] . "<br>";
}
$name = $_POST['name'];
 $age = $_POST['age'];

for($l=0; $l < count($name); $l++)
 { //your condition
}

不要忘记将此添加到您的 html

 <label>Name</label>
  <input type="text" size="20" name="name[]" id="name"   >
     </td>
  <td>
 <label>Age</label>
<input type="text" size="20" name="age[]" id="age"  >
   </td>