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. Commented 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. Commented Sep 3, 2012 at 10:17
  • @user1642610 You should see the answer made by SMka Commented Sep 3, 2012 at 10:18
  • One could use: file_get_contents('php://input'); to bypass php's default behaviour. Commented Sep 3, 2012 at 10:20
  • @user1642610: too many updates. think twice what u want Commented 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/>
Sign up to request clarification or add additional context in comments.

7 Comments

i have totaly 3 set's of the above input box but how can i seperate the first input box value
echo $_POST['passname']['name'][0] :)
not working i get the value as this i am num 0 and my value varun i am num 1 and my value kumar
not working value i got i am num passname and my value i am num send and my value S
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/>'; }
|
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'];
?>

Comments

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 Comments

without the above mentioned type is possible
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.

Your Answer

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