4

i have a input elements as below

        <form method="post" >
1       <input type="text" name="passname[name]" /> <br/> <br/>
2       <input type="text" name="passname[type]" /> <br/> <br/>
3       <input type="text" name="passname[age]" /> <br/> <br/>

1       <input type="text" name="passname[name]" /> <br/> <br/>
2       <input type="text" name="passname[type]" /> <br/> <br/>
3       <input type="text" name="passname[age]" /> <br/> <br/>

        <input type="submit" name="send" value="Submit" /> <br/>
    </form>

i want all the text box as array in php

like Below

Passenger Name : passname[name]
Passenger Type : passname[type]
Passenger Age : passname[age]

Is this possible with php

form code used in form

<input type="text" name="passname[name][]" /> <br/> <br/>
<input type="text" name="passname[type][]" /> <br/> <br/>
<input type="text" name="passname[age][]" /> <br/> <br/>

<input type="text" name="passname[name][]" /> <br/> <br/>
<input type="text" name="passname[type][]" /> <br/> <br/>
<input type="text" name="passname[age][]" /> <br/> <br/>

when i submit the form i get this type of array value

    array
  'passname' => 
        array
          'name' => 
            array
              0 => string '1' (length=1)
              1 => string '1' (length=1)
          'type' => 
            array
              0 => string '2' (length=1)
              1 => string '2' (length=1)
          'age' => 
            array
              0 => string '3' (length=1)
              1 => string '3' (length=1)
      'send' => string 'Submit' (length=6)

how can i do it in for each like blow

<?php
if(isset($_POST['aa']))
{
    $number = $_POST['1'];
    $text = $_POST['2'];
    foreach($number as $key=>$val)
    {
        echo $val."=".$text[$key]."<br />";
    }
}
?>
<formid="a"name="a"method="post">
<selectid="1[]"name="1[]">
<optionvalue="1">1</option>
<optionvalue="3">3</option>
<optionvalue="2">2</option>
</select>
<inputtype="text"value=""id="2[]"name="2[]"/>
<selectid="1[]"name="1[]">
<optionvalue="1">1</option>
<optionvalue="3">3</option>
<optionvalue="2">2</option>
</select>
<inputtype="text"value=""id="2[]"name="2[]"/>
<inputtype="submit"id="aa"name="aa"value="send now"/>
</form>
5
  • It's got nothing to do with PHP. A browser will not pass an array that is not a checkbox. The value in the second text input with the name of name="passname[name]" will overwrite the first value passed from the form.
    – Fluffeh
    Sep 3, 2012 at 10:15
  • No, the browser will happily send all input fields. PHP will overwrite the array element with the last value upon parsing the request.
    – lanzz
    Sep 3, 2012 at 10:17
  • @user1642610 You should see the answer made by SMka
    – Fluffeh
    Sep 3, 2012 at 10:18
  • One could use: file_get_contents('php://input'); to bypass php's default behaviour.
    – Yoshi
    Sep 3, 2012 at 10:20
  • @user1642610: too many updates. think twice what u want
    – SMka
    Sep 3, 2012 at 10:34

3 Answers 3

7
<input type="text" name="passname[name][]" /> <br/> <br/>
<input type="text" name="passname[type][]" /> <br/> <br/>
<input type="text" name="passname[age][]" /> <br/> <br/>
7
  • i have totaly 3 set's of the above input box but how can i seperate the first input box value
    – user1617426
    Sep 3, 2012 at 10:19
  • echo $_POST['passname']['name'][0] :)
    – SMka
    Sep 3, 2012 at 10:21
  • not working i get the value as this i am num 0 and my value varun i am num 1 and my value kumar
    – user1617426
    Sep 3, 2012 at 10:30
  • not working value i got i am num passname and my value i am num send and my value S
    – user1617426
    Sep 3, 2012 at 10:35
  • this is working foreach ($_POST['passname'] as $key => $val) { echo 'My Name Is ' . $val['name'] . ' and My Gender is '.$val['type'].' my Age is ' . $val['age'] . '<br/>'; }
    – user1617426
    Sep 3, 2012 at 10:40
6
<form method="post" >
       <input type="text" name="passname[name][]" /> <br/> <br/>
       <input type="text" name="passname[type][]" /> <br/> <br/>
       <input type="text" name="passname[age][]" /> <br/> <br/>

       <input type="text" name="passname[name][]" /> <br/> <br/>
      <input type="text" name="passname[type][]" /> <br/> <br/>
       <input type="text" name="passname[age][]" /> <br/> <br/>

        <input type="submit" name="send" value="Submit" /> <br/>
    </form>

<?php
$_POST['passname']['name'];
$_POST['passname']['type'];
$_POST['passname']['age'];
?>
0

If you want to keep the passenger separated, you should build your form like this:

<form method="post" >
  <input type="text" name="passname[0][name]" /> <br/> <br/>
  <input type="text" name="passname[0][type]" /> <br/> <br/>
  <input type="text" name="passname[0][age]" /> <br/> <br/>

  <input type="text" name="passname[1][name]" /> <br/> <br/>
  <input type="text" name="passname[1][type]" /> <br/> <br/>
  <input type="text" name="passname[1][age]" /> <br/> <br/>

  <input type="submit" name="send" value="Submit" /> <br/>
</form>

then, once submited, your $_POST array will look like this one:

$_POST == array(
  0 => array(
    'name' => 'XXX',
    'type' => 'XXX',
    'age' => 'XXX',
  ),
  1 => array(
    'name' => 'XXX',
    'type' => 'XXX',
    'age' => 'XXX',
  ),
  'send' => 'Submit',
);

where XXX represents the user input data.

2
  • without the above mentioned type is possible
    – user1617426
    Sep 3, 2012 at 10:22
  • Is this a question or a comment? Anyway, if you do not require the type parameter, simply remove it from the form and the posted data will modify accordingly. Sep 3, 2012 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.