Post text box array in PHP

23,039

Solution 1

<input type="text" name="array[]" />
<input type="text" name="array[]" />
<input type="text" name="array[]" />
<input type="text" name="array[]" />


print_r( $_POST['array'] );

Solution 2

<input type="text" name="ItemName[1][2]" >
<input type="text" name="ItemName[1][3]" >
<input type="text" name="ItemName[1][4]" >

$ItemNamesArray = $_POST['ItemName'][1];

foreach($ItemNamesArray as $item){
  var_dump($item); //this will show you the value of each item

 // do whatever you want to do (insert into a database, send an email, etc)
}

But I would not use a bidimensional array for this, only if ItemName is been used for another purpose on the same form.

Solution 3

<form method="post" name="myform">
<input type="text" name="array[]" Value="101"/>
<input type="text" name="array[]" Value="102"/>
<input type="text" name="array[]" Value="103"/>
<input type="text" name="array[]" Value="104"/>
<input type="submit" name="submit" Value="submit"/>
</form>



if(isset($_POST['submit'])){

foreach($_POST['array'] as $myarray) {

    echo $myarray.'<br>';

}

OUTPUT

101
102
103
104
Share:
23,039
sqlchild
Author by

sqlchild

Making me.....will appear soon :) Work : PHP and MySQL are in my two hands, shaking my hands together, thus, INNER JOINING the two entities to output some good web applications Need to Say me Hi!....just post a comment on any of my SO post....I will be right there :)

Updated on August 15, 2020

Comments

  • sqlchild
    sqlchild over 3 years

    I have text box values to be posted . How do I take it in a PHP array .

    EDIT

    ---------------------------
        <input type="text" name="ItemName[1][2]" >
            <input type="text" name="ItemName[1][3]" >
            <input type="text" name="ItemName[1][4]" >
    ------------------------------
    $ItemNamesArray = $_POST[] ..... ????? What do I do in this step???
    

    Please help.

  • pawel-kuznik
    pawel-kuznik about 12 years
    Should also write that he can access it via $_POST['array'][0], $_POST['array'][1], ... cause trick with [] will do an array in PHP.
  • sqlchild
    sqlchild about 12 years
    sir, i directly pasted the html which was hidden ,now i have recdtified the post ... please help now
  • sqlchild
    sqlchild about 12 years
    sir, i directly pasted the html which was hidden ,now i have recdtified the post ... please help now
  • s.webbandit
    s.webbandit about 12 years
    Not so good format. Can you change names of your inputs or it's impossible?
  • Apolo
    Apolo almost 10 years
    OP does not talk about database
  • Apolo
    Apolo almost 10 years
    OP does not talk about database
  • Apolo
    Apolo almost 10 years
    This will output '3', and does not give an array