4

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.

sqlchild
  • 8,754
  • 28
  • 105
  • 167

9 Answers9

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


print_r( $_POST['array'] );
scibuff
  • 13,377
  • 2
  • 27
  • 30
  • Should also write that he can access it via $_POST['array'][0], $_POST['array'][1], ... cause trick with [] will do an array in PHP. – pawel-kuznik Apr 23 '12 at 11:25
  • sir, i directly pasted the html which was hidden ,now i have recdtified the post ... please help now – sqlchild Apr 23 '12 at 11:25
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.

romulodl
  • 91
  • 1
  • 1
  • 7
1
<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
Waruna Manjula
  • 3,067
  • 1
  • 34
  • 33
0

Your POST input is in $_POST array. To display it - var_dump($_POST). To access its items - for example named 'textbox' - var_dump($_POST['textbox'];

s.webbandit
  • 16,332
  • 16
  • 58
  • 82
0

Try this

$_REQUEST['ItemName[1][2]'];
$_REQUEST['ItemName[1][3]'];
$_REQUEST['ItemName[1][4]'];
vicky
  • 695
  • 5
  • 15
0

Try :

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

and input to database using insert SQL :

// EXECUTE SQL INDEXED TEXTBOX
foreach ($_POST['itemName'] as $item)
{

$query  = "INSERT INTO  tableName (field1)";
$query .= " VALUES ('" .  $item . "')";

}
mrsnax
  • 11
  • 1
0

Here is what I have done to show you what happen when you submit :

I completed your script to show the $_POST var (what you click on submit) like so :

<form method="POST">
  <input type="text" name="ItemName[1][2]" value="a">
  <input type="text" name="ItemName[1][3]" value="b">
  <input type="text" name="ItemName[1][4]" value="c">
  <input type="submit">
</form>

<?php
if($_POST) {
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";
}
?>

Here is the output when I click on submit :

Array
(
    [ItemName] => Array
        (
            [1] => Array
                (
                    [2] => a
                    [3] => b
                    [4] => c
                )
        )
)

It show you that you have your array of values in $_POST["ItemName"][1] so you can do :

$myArrayOfValues = $_POST["ItemName"][1];

I hope this will help you.

Apolo
  • 3,844
  • 1
  • 21
  • 51
-1

$_POST or $_GET is a array which the way the user can interact with web form. In this case, I have a sample form:

<form method="POST" action="array.php">

 <input name="a" type="text" value="1" />
 <input name="b" type="text" value="2" />
 <input type="submit" value="Sum" />

action form array.php:

<?php   
    $a = $_POST['a'];
    $b = $_POST['b'];
    echo $a + $b;
?>

$_POST['a'],$_POST['b'] is variable bring value of textbox, you can use in PHP Code to evaluate.

-1

Try this

Form:

<input type="text" name="ItemName[]" value="">

PHP Script:

$ItemName = POST['ItemName'];

for ($i=0; $i<sizeof($ItemName); $i++){

   $sq = mysql_query("SELECT * FROM `table` WHERE `ItemName`='$ItemName[$i]'");

}
Jason
  • 33
  • 7