user11211072
user11211072

Reputation:

How to send a PHP array via HTML form (using input tag)

I would like to send an array in the input form element.

My array:

Array( [0] => 55 [1] => 1 [2] => 4 )

HTML <input> tag:

<input type="hidden" name="category" value="'.$category.'" />

This is the URL my code resulted in:

http://localhost/search?&category%5B%5D=55&category%5B%5D=1&category%5B%5D=4

But I also get this PHP Notice:

PHP Notice: Array to string conversion

How do I send an array via HTML form correctly?

============================

I found this solution:

foreach ($category as $item) {
    echo '<input type="hidden" name="categories[]" value="'.$item.'" />';                       
}

Prepare your array $category. Example:

    echo '<div align="center">  
    <form method="GET" action="/search" />                      
    <input type="hidden" name="search" value="'.$search.'" />'; 

    foreach ($category as $item){

    echo '<input type="hidden" name="categories[]" value="'.$item.'" />'; 

    } 

    echo '<input type="submit">        
    </form>
    </div>';

And get:

if(isset($_GET['categories'])) {

$categories = $_GET['categories'];                    

}

Upvotes: 2

Views: 2511

Answers (3)

Weyroxow
Weyroxow

Reputation: 11

The accepted answer is correct, however if you want to use $_POST data instead of $_GET data; You must use textarea as follows;

echo '<textarea name="category" id="category">"' .json_encode($category) . '"</textarea>';

... and when you send your form, parse the JSON back to the array:

$category = json_decode($_POST["category"]);

Upvotes: 0

Shah Fahad
Shah Fahad

Reputation: 227

Sending a dynamic php array via hidden html input field through form. Here's how to do it.

1. Suppose this is the data you want to post

<?php
    // Dynmaic array
    $items = array(0 =>
                        array(
                            'Name'         => 'Colored Pencil',
                            'Description'  => 'Test description',
                            'Quantity'     => 1,
                            'Price'        =>
                                array(
                                    'Amount' => 2, //value
                                    'Type'   => 'CUSTOM',
                                     )
                             )
                    );

    // Serialize the array to JSON
    $productData = json_encode($items );
?>

2. Setup a form

    // Form submit items plus other information
    <form action="items_data.php" method="POST">
        // Other Details...
        <input type="hidden" name="items_data" id="items_data">
        <input type="submit" value="Send">
    </form>

3. Set value to hidden tag via .js

    // Add the script
    <script>
        // Set the serialized data to the hidden input field
        document.getElementById('product_data').value = <?php echo json_encode($productData); ?>;
    </script>

4. Recieve the data

<?php
    // Recieve the data in 'items_data.php' file and Decode after submission
    $productArray = json_decode($_POST['product_data'], true);

    // print data
    echo("<pre>");
    print_r($productArray);
    echo("</pre>");
?>

EXPLANATION

If the array is dynamically generated by PHP code. In order to send such array via form using hidden input. The first thing is to serializes this array to a JSON string using 'json_encode'. The javascript is used for echoing and setting the value of hidden input field with the name "items_data" within the HTML script. After gathering other information thourgh form and submitting it, the JSON data will be sent to the server-side script. At the server side the data can then be retrieved and then parsed the JSON data.

Note: To ensure that the decoded data is an associative array, you can use the json_decode function with the true parameter and JavaScript, is used to set the value of the hidden input field dynamically.

Upvotes: -1

Genhis
Genhis

Reputation: 1524

The easiest way to do that is to convert your categories array to JSON and back.

echo '<input type="hidden" name="category" value="' . json_encode($category) . '" />';

... and when you send your form, parse the JSON back to the array:

$category = json_decode($_GET["category"]);

Upvotes: 5

Related Questions