IB Gd Sarasvananda
IB Gd Sarasvananda

Reputation: 11

Cannot POST a multi-array from FORM to process PHP

When I created form input using a two-dimensioned array, but cannot read in process POST

<form name=\"FormAdd\" id=\"FormAdd\" role=\"form\" method=\"post\"  
                action=\"?page=".$page."&language=".$language."&action=adddata\" enctype=\"multipart/form-data\">
                <input type=\"text\" name=\"am[0][0]\" value=\"23\">
                <button type=\"submit\" class=\"btn btn-primary\"><i class=\"fa fa-floppy-o\" aria-hidden=\"true\"></i> ".SAVE."</button>
            </form>


print_r($am);

Result Array ( [0] => )

Should result Array ( [0] => Array ( [0] => 23 ) )

Upvotes: 0

Views: 37

Answers (2)

Flash Thunder
Flash Thunder

Reputation: 12036

The problem is that you are reading it incorrectly in PHP, you should use:

print_r($_POST['am']);

instead of

print_r($am);

That should work fine.

Live example.

Upvotes: 0

ash__939
ash__939

Reputation: 1599

If I understood your question correctly, you want to post multiple input values for the field am.

The below form will post multiple values for am field.

<form name="FormAdd" id="FormAdd" role="form" method="post"  
action="?query=WHAT_EVER_YOUR_QUERY_STRING" enctype="multipart/form-data">
    <input type="text" name="am[]" value="23">
    <input type="text" name="am[]" value="24">
    <input type="text" name="am[]" value="25">
    <input type="text" name="am[]" value="26">
    <input type="text" name="am[]" value="27">
    <button type="submit" class="btn btn-primary"><i class="fa fa-floppy-o" aria-hidden="true"></i> ".SAVE."</button>
</form>
<?php
if ($_POST['am']) {
    print_r($_POST['am']);
}

OUTPUT

Array
(
    [0] => 23
    [1] => 24
    [2] => 25
    [3] => 26
    [4] => 27
)
  1. Notice the [] symbol in the name of the field in HTML form.
  2. Notice the [] is not needed while accessing the POST values in PHP file.

Similarly, if you use a form something like:

    <input type="text" name="am[0][]" value="23">
    <input type="text" name="am[0][]" value="24">
    <input type="text" name="am[0][]" value="25">
    <input type="text" name="am[0][]" value="26">
    <input type="text" name="am[0][]" value="27">

The output would be,

Array
(
    [0] => Array
        (
            [0] => 23
            [1] => 24
            [2] => 25
            [3] => 26
            [4] => 27
        )

)

Upvotes: 1

Related Questions