如何获取同名多个文本输入的值

时间:2022-11-29 13:02:03

i have just created one form that have some input fields with the same name . i want store all those value in one variable after submitting a form .

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

the code is

代码是

      <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>`

how to store this value in one variable .? please help i am new to php

如何将该值存储在一个变量中?请帮助我了解php

2 个解决方案

#1


3  

$name = $_POST['name'];
 $age = $_POST['age'];

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

don't forget to add this in your html

不要忘记在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>

#2


2  

Create an array using [] for all similar names:

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

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

And then you can iterate over them:

然后你可以对它们进行迭代:

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

#1


3  

$name = $_POST['name'];
 $age = $_POST['age'];

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

don't forget to add this in your html

不要忘记在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>

#2


2  

Create an array using [] for all similar names:

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

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

And then you can iterate over them:

然后你可以对它们进行迭代:

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