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>
1
  • Hi, Can you clarify your problem? Commented Jun 19, 2019 at 9:49

6 Answers 6

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

Comments

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

Comments

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

Comments

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

Comments

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>

Comments

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.