2

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

}
5
  • 2
    Converting the array to JSON and sending it like that to your server would probably be a better approach. Commented Jul 21, 2019 at 18:46
  • I can use serialize() and unserialize() ? Commented Jul 21, 2019 at 19:50
  • @polyman Thank you for posting the solution you found. It would be even better if you could write it as an answer explaining it. Commented Jul 21, 2019 at 21:40
  • It depends on the array, but in this example, foreach protrudes, as you can see. $ category is an array like above. Commented Jul 21, 2019 at 22:31
  • Does this answer your question? Passing an array using an HTML form hidden element Commented Oct 13, 2023 at 12:12

3 Answers 3

5

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

Comments

0

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"]);

1 Comment

Please share more details. Why should changing from a common input field to a textarea be neccessary?
-1

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.

1 Comment

Please add some explanation to your answer such that others can learn from it. Why do you need to use JS for this?

Your Answer

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