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.

3
  • use json to post, and json_encode to retreive the data Commented Apr 23, 2012 at 11:20
  • why json? Is there any sense in that? Commented Apr 23, 2012 at 11:23
  • I directly pasted the html which was hidden ,now i have rectified the post ... please help now Commented Apr 23, 2012 at 11:26

9 Answers 9

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'] );
Sign up to request clarification or add additional context in comments.

2 Comments

Should also write that he can access it via $_POST['array'][0], $_POST['array'][1], ... cause trick with [] will do an array in PHP.
sir, i directly pasted the html which was hidden ,now i have recdtified the post ... please help now
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.

Comments

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

Comments

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'];

2 Comments

sir, i directly pasted the html which was hidden ,now i have recdtified the post ... please help now
Not so good format. Can you change names of your inputs or it's impossible?
0

Try this

$_REQUEST['ItemName[1][2]'];
$_REQUEST['ItemName[1][3]'];
$_REQUEST['ItemName[1][4]'];

Comments

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 . "')";

}

1 Comment

OP does not talk about database
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.

Comments

-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 Comment

This will output '3', and does not give an array
-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]'");

}

1 Comment

OP does not talk about database

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.