Vaibhav Shettar
Vaibhav Shettar

Reputation: 810

pass php multidimensional array from html hidden field to php

we want to pass php array in hidden input field from html to php. how can we pass the array from html to php

    <?php 
        $data = array(
         array("Volvo",22,18),
         array("BMW",15,13),
         array("Saab",5,2),
         array("Land Rover",17,15)
       );
    ?>


    <form name="excel_upload" id="excel_upload" action="" method="post">
    <input type="hidden" name="data[]"  >
    <input type="submit">
    </form>

Upvotes: 0

Views: 983

Answers (6)

Vaibhav Shettar
Vaibhav Shettar

Reputation: 810

finally after trying lot of method this one worked for me with using serialize and unseiralize.

HTML:

<input type="hidden" name="data" value="<?php echo htmlspecialchars(serialize($data)) ?>">

PHP:

$excel = unserialize($_POST["data"]);

Upvotes: 0

Amarat
Amarat

Reputation: 630

Try this :

<!DOCTYPE html>
<html lang="en">
    <?php
    $data = array(
        array("Volvo", 22, 18),
        array("BMW", 15, 13),
        array("Saab", 5, 2),
        array("Land Rover", 17, 15)
    );

    if (isset($_POST['submit'])) {
        print_r($_POST['data']);
    }
    ?>
    <body>
        <form method="post" action="">
            <?php
            foreach ($data as $key => $val) {
                foreach ($val as $k => $v) {
                    ?>
                    <input type="hidden" name="data[<?php echo $key; ?>][<?php echo $k ?>]" value="<?php echo $v; ?>">
                    <?php
                    echo "<br>";
                }
            }
            ?>
            <input type="submit" name="submit">
        </form>
    </body>
</body>
</html>

Upvotes: 0

Ashutosh Kamble
Ashutosh Kamble

Reputation: 193

You can't send array as it is but you can achieve same thing using some thing like

<form name="excel_upload" id="excel_upload" action="" method="post">
    <?php 
       $i = 1;
       foreach($cars as $car){

    ?>  
    <input type="hidden" name="car<?php echo $i; ?>" value="<?php $car; ?>" >
    <?php
          $i++;
      }
    ?>
    <input type="submit">
</form>

You can get value on server side in same way like $_POST['car1'] etc

Upvotes: 0

Er. Anurag Jain
Er. Anurag Jain

Reputation: 1793

Best way to pass Array from a form is sending in serialize format.

<form name="excel_upload" id="excel_upload" action="" method="post">
<input type="hidden" name="data" value="<?php echo serialize($cars); ?>" >
<input type="submit">
</form>

Once Form Post, You can unserialize the posted value.

<?php
  $data  = isset($_POST['cars']) ? unserialize($_POST['cars']) : [];
?>

You can also use json_encode in place of serialize and get data by json_decode. in place of unserialize

Thanks

Upvotes: 0

Shivani Sonagara
Shivani Sonagara

Reputation: 1307

 <?php 
 $cars = array("Volvo", "BMW", "Toyota");
 $cars_string = implode(',',$cars);
 ?>

<form name="excel_upload" id="excel_upload" action="" method="post">
    <input type="hidden" name="data" value="<?php echo $cars_string?>"  >
    <input type="submit">
</form>

You may use implode function of PHP. To decode it at php end again use explode function of PHP

Upvotes: 1

Adi Fatol
Adi Fatol

Reputation: 964

You can have multiple hidden inputs with the same name and if the name contains the brackets, PHP will parse them as an array. See this answer for more details: HTML form with multiple hidden control elements of the same name

For example:

    <form name="excel_upload" id="excel_upload" action="" method="post">
    <input type="hidden" name="data[]" value="Volvo" >
    <input type="hidden" name="data[]" value="BMW" >
    <input type="hidden" name="data[]" value="Toyota" >
    <input type="submit">
    </form>

Upvotes: 1

Related Questions