0

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>
Vaibhav Shettar
  • 790
  • 2
  • 9
  • 20

6 Answers6

1

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>
Adi Fatol
  • 956
  • 15
  • 24
1
 <?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

Shivani Sonagara
  • 1,299
  • 9
  • 21
0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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

Er. Anurag Jain
  • 1,780
  • 1
  • 11
  • 19
0

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>
Amarat
  • 602
  • 5
  • 11
0

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"]);
Vaibhav Shettar
  • 790
  • 2
  • 9
  • 20