Reputation: 339
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.
Upvotes: 2
Views: 8214
Reputation: 1646
Use my_form_data[]
in name
<form method="post" action="next.php">
<input type="hidden" name="my_form_data" value="<?php echo htmlentities(serialize($my_arr));?>">
</form>
Upvotes: 0
Reputation: 42707
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"]);
Upvotes: 4
Reputation: 1661
$hiddenvariable=array("apple","banana","cat");
foreach($hiddenvariable as $value)
{
echo '<input type="hidden" name="my_form_data[]" value="'. $value. '">';
}
Making an array first then extracting an each elements using foreach and passing those values into hidden value.
Upvotes: 3
Reputation: 34914
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>
Upvotes: 3