Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get value of Multiple text inputs with same name

Tags:

html

forms

php

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

like image 389
Nayana Avatar asked Jan 06 '15 07:01

Nayana


2 Answers

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

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

don't forget to add this in your 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>
like image 53
mani Avatar answered Nov 04 '22 17:11

mani


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>";
}
like image 41
Dev01 Avatar answered Nov 04 '22 18:11

Dev01