Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass php array in input type hidden in html form

Tags:

html

forms

php

I want to pass my php array in my form input type as hidden.i have tried many times but it is giving me an array (key=>value,key=>value) in my input form.

This is my php code have a array.

 $my_arr = array();
 $my_arr['key']="value"; 

This is my html code

 <form method="post" action="next.php">
 <input type="hidden" name="my_form_data" value="<?php print_r($my_arr) ?>">
 <button name="submit_btn">Submit</button>
</form>

Any one please help me to pass php array in my input hidden element and how to get it in next page.

like image 503
bharat savani Avatar asked Jun 10 '26 20:06

bharat savani


2 Answers

If I'm understanding you correctly, you can try this:

<form method="post" action="next.php">
 <input type="hidden" name="my_form_data" value="<?php echo htmlspecialchars(serialize($my_arr)) ?>">
 <button name="submit_btn">Submit</button>
</form>

Then in next.php you unserialize to get the PHP data structure back:

<?php
$my_arr = unserialize($_POST["my_form_data"]);
like image 67
miken32 Avatar answered Jun 13 '26 10:06

miken32


The easiest way is to make json as string by json_encode and then decode at the time of retrieve by json_decode,

<form method="post" action="next.php">
<input type="hidden" name="my_form_data" value="<?php echo json_encode($my_arr); ?>">
<button name="submit_btn">Submit</button>
</form> 
like image 30
Niklesh Raut Avatar answered Jun 13 '26 11:06

Niklesh Raut



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!