2

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.

1
  • i have limit to pass one hidden element only @VimalS Commented Mar 10, 2017 at 6:07

4 Answers 4

4

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

Comments

3

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> 

Comments

3
$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.

3 Comments

This is something same as : stackoverflow.com/questions/6547209/…
I have just modified the code according to his need mate I haven't searched on stackoverflow and read all those duplicate answers. Gave him answer according to this need :)
My positive vote to this answer! It's simple and don't need changes in the root form. good head! Very usefull if you have a form with select multiple and link in the results to a profile, and a back button to the form, because this answer make the array like a multiple select.
0

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>

1 Comment

@bharat-savani I changed my answer

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.