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

31 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. 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. 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.

  8. Jean-Marc says:

    Hello there,
    I encounter the same problem but on a legacy PHP extranet where register_globals=On is set in /etc/php.ini, I know it is not hacker-proof but no time to refactor the whole code,
    I have my input fields in the same way described above that is:


    However i can not get back the values in the response page through:
    $_POST[‘$myTab’];
    or directly with $myTab (thanks to globals),

    Anyone for leads?

    Cheers,

    Jean-Marc

  9. PhpCatalog says:

    It’s not working…

  10. brenda says:

    I really liked reading this post

  11. What a great post highlighting a unique trick of PHP to integrate so closely with HTML form elements

  12. […] searched the world wide web looking for a solution, and this looked promising, but I do not know if it's feasible considering that my arrays contain other […]

  13. Dante says:

    This post helped me a LOT : )
    Thank you

  14. Baera says:

    I do this with “[]” at the end of form variable name but how is the array inserted into the form? I have to put date back into the form in hidden element to send again after user confirm. What is PHP native form array encoding?

  15. Trivikram says:

    Thanks Buddy,
    Just got what I am looking for. It helped me a lot. Thanks Again.

  16. IvaEspn says:

    Hi:
    I’m sure that my problem is simple, but i’m just a beginner.
    i have this array:
    $array=array(“a”,”b”,”c”);
    i’d like to send via input

    please help me

  17. prabu says:

    It’s not working…. my array is not fetching in the next page

  18. ezhhhhh says:

    Thanx (^___^)

  19. Rakesh says:

    Nice article …working cool..

  20. nice blog thanks for sharing information. keep writing…….. 😉

    learn php

  21. Nice and great post this is very useful for us keep blogging. and thanks for share information with us.
    php string variables

  22. morussxxx says:

    http://extube.net/content_preview/ero3/amateur/ricons.jpg
    нажать и смотреть

    скачать прно на мобильный
    скaчaть прно нa мобильный
    прно скачать на мобильный телефон
    скачать прно видео на мобильный
    скачать прно ролики для мобильного
    мобильные прно фильмы скачать
    прно 3gp на мобильный скачать
    прно для мобильного скачать торрент
    скачать прно снятое на мобильный
    скачать прно mp4 на мобильный
    скачать прно в мобильном формате
    скачать прно игры на мобильный
    прно фото для мобильного скачать
    скачать прно на мобильный русское
    скачать прно картинки на мобильный
    скачать частное мобильное прно
    скачать прно на мобильный азиатки
    короткое прно скачать на мобильный
    прно скачать на мобильный онлайн
    скачать гей прно на мобильный
    скачать прно клипы для мобильного
    скaчaть прно нa мобильный
    скaчaть прно нa мобильный
    скaчaть прно нa мобильный
    прно скачать на мобильный телефон
    скачать прно видео на мобильный
    скачать прно ролики для мобильного
    мобильные прно фильмы скачать
    прно для мобильного скачать торрент
    скачать прно снятое на мобильный

  23. Thanks a lot, it has work form me.

  24. raj says:

    <?php
    if($_POST['submit'] == 'Submit')
    {

    for($j=0;$j<$count;$j++)
    { $p1=$_POST['date6[j]'];
    $p2=$_POST['date7[j]'];
    $p3=$_POST['t[j]'];
    $p4=$_POST['id[j]'];

    $re= mysql_query("update request set fromdate= '$p1', todate='$p2',status='$p3' where eid='$p4'") or die(mysql_error());echo '

    '; print_r($_POST); exit;
    	}
    }
    ?>
    
    
    
    
    
    
    
    <form name="report" action="" method="post">
    
    
    Emp Id 
    Emp Name 
    Department
    From 
    To
    Type
    change From 
    change To 
    Staus
    
    
    
    
    
    
    
     
    
    setIcon("calendar/images/iconCalendar.gif");
    	  $myCalendar->setDate(date('d'), date('m'), date('Y'));
    	  $myCalendar->setPath("calendar/");
    	  $myCalendar->setYearInterval(2000, 2015);
    	  $myCalendar->dateAllow('2008-05-13', '2015-03-01');
    	  $myCalendar->setDateFormat('j F Y');
    	  $myCalendar->setAlignment('left', 'bottom');
    	  $myCalendar->setSpecificDate(array("2011-04-01", "2011-04-04", "2011-12-25"), 0, 'year');
    	  $myCalendar->writeScript();
    	  ?>
    	  setIcon("calendar/images/iconCalendar.gif");
    	  $myCalendar->setDate(date('d'), date('m'), date('Y'));
    	  $myCalendar->setPath("calendar/");
    	  $myCalendar->setYearInterval(2000, 2015);
    	  $myCalendar->dateAllow('2008-05-13', '2015-03-01');
    	  $myCalendar->setDateFormat('j F Y');
    	  $myCalendar->setAlignment('left', 'bottom');
    	  $myCalendar->setSpecificDate(array("2011-04-01", "2011-04-04", "2011-12-25"), 0, 'year');
    	  $myCalendar->writeScript();
    	  ?>
     
      Pending
      Denied
      Approved
      Approved with changes
      
     <input type="hidden" name="id[]" value="">
    	
     
     
     
    
    
    
  25. raj says:

    can any one help me how to i get update above query using arry and post,as im new to php….

  26. Alright, totally got it now, thanks raj!

  27. Я знаю где купить новый 5 айфон iphone 5 купить в россии с Доставкой во все города Росси.

  28. Excellent information.Hope more in future.

Leave a reply to Abhijeet Pathak Cancel reply