Passing Array Using Html Form Hidden Element

Html form hidden element is used to pass information which is not visible to the user but used internaly by underlying scripts. For example the status of the process or step of multi step form submission. PHP array is an important language structure used in manipulation of multiple values associated with single object. Sometimes it is required to pass these values accross the pages between browser and the server. Following method can be used to pass PHP Arrays using html form hidden elements.

Using individual array element

This method uses seperate hidden element for each element in array. This can be done by following method.

foreach ($my_array as $key => $value)
{
 echo ‘<input type=hidden name=”my_array[]” value=”‘.htmlspecialchars($value).’”>’;
}

This will generate an array in html form. And when submitted can be accessed by form handling PHP script using following code.
$my_array = $_POST['my_array'];

Combining all array elements in one value to hidden element

In this method all elements in an array are combined in single value using implode() PHP function. implode() will return a string combining all the array elements in the same order, seperated by given string. Then this combined value can be passed in html form hidden element. See below.
$single_value = implode(“,”, $my_array);
echo ‘<input type=hidden name=”single_value” value=”‘.htmlspecialchars($single_value).’”>’;

And when submitted the array can be retreived using explode() PHP function. explode() will return an array by spliting the string using the given seperator. See code below.
$my_array = explode(“,”,$_POST['single_value']);

9 Responses to “Passing Array Using Html Form Hidden Element”

  1. Mihai G Says:

    Hey there, nice blog …
    I’m using a different approach on passing data from page to page.
    You can serialize the form data and store it in a SESSION variable.

    • Vasava Nishant Says:

      Thanks mithai, nice to c u here..

      Well, My problem is little bit more complex, I have multivalue for few fields and the size can reach as big as 300. In that case, I prefer not to put it in SESSION. Also, I have no access to database. The queries are related to LDAP.. no possibility to add it to database.

      In my case, I would use javascript to concatenate the values in a single string using separator and then pass it to parameter.

  2. Abhijeet Pathak Says:

    Yes, you can use this approach or use $_SESSION variable or if you have a lot of data then you can actually use a database for storing this session related information…

  3. Craig Smith Says:

    Thank You!!!!!!!!!!!!!!!!!!!!!!!!!!!

  4. tim b Says:

    i am trying to display a form with a select element to which the users select a value from the list. this is working well however on reflection i have a relational db which stores the required info as such i want to use the key id which relates to the selected value to echo to another form.

    My question is this how do i get the hidden value to be that of the table column that i require so that i can echo it into a variable??

    I can sort of see the solution in the one above but need a bit more help please

    regards newbie php user

    tim

  5. bharat Says:

    Hi,
    my target is as such

    1. i have a form in which their is a table and in each row of that table we have to put some values which comes from dropdown and text name.also their is column for file upload .
    i am sending the code here of table

    <?php
    $sql = “SELECT pkCountryID,CountryID,CountryName FROM country_info ORDER BY CountryName ASC”;
    $country = mysql_query($sql);
    while($row = mysql_fetch_array($country))
    {
    echo (“$row[CountryName]“);
    }
    ?>

    <?php
    $sql = “SELECT pkSubjectID,SubjectID,SubjectName FROM subject_info ORDER BY SubjectName ASC”;
    $subject = mysql_query($sql);
    while($row = mysql_fetch_array($subject))
    {
    echo (“$row[SubjectName]“);
    }
    ?>

    PaperNote
    YesNo

    <?php
    $sql = “SELECT pkCountryID,CountryID,CountryName FROM country_info ORDER BY CountryName ASC”;
    $country = mysql_query($sql);
    while($row = mysql_fetch_array($country))
    {
    echo (“$row[CountryName]“);
    }
    ?>

    <?php
    $sql = “SELECT pkSubjectID,SubjectID,SubjectName FROM subject_info ORDER BY SubjectName ASC”;
    $subject = mysql_query($sql);
    while($row = mysql_fetch_array($subject))
    {
    echo (“$row[SubjectName]“);
    }
    ?>

    PaperNote
    YesNo

    2. my target is to insert the each rows value in the database and uplaod the file in a folder
    3. i am getting the problem in arranging the arrays values during insert query

  6. mike s Says:

    thanks, these are great – but what about if your array keys are named (instead of 0, 1, 2) – is there a way to quickly modify the above code ( i’m more interested in the 1st example ) so that your array does not lose the key names? thanx in advance, mike

  7. Марк Says:

    Фильмы онлайн,смотреть онлайн фильмы,фильмы онлайн бесплатно. Фильмы смотреть бесплатно онлайн,фильмы онлайн без регистрации,фильмы онлайн бесплатно без регистрации,смотреть фильмы онлайн без регистрации,онлайн фильмы качество,порно фильмы онлайн,онлайн фильмы хорошого качества,онлайн фильмы хорошего качества,смотреть онлайн фильмы в качестве,смотреть хорошие фильмы онлайн,смотреть фильмы онлайн хорошего качества,онлайн фильмы качество бесплатно,фильмы онлайн смотреть бесплатно качество

  8. Vasava Nishant Says:

    Thanks mithai, nice to c u here..

    Well, My problem is little bit more complex, I have multivalue for few fields and the size can reach as big as 300. In that case, I prefer not to put it in SESSION. Also, I have no access to database. The queries are related to LDAP.. no possibility to add it to database.

    I would use javascript to concatenate the values in a single string using separator and then pass it to parameter.

Leave a Reply