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’]);
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.
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.
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…
Thank You!!!!!!!!!!!!!!!!!!!!!!!!!!!
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
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
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
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.
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
It’s not working…
I really liked reading this post
What a great post highlighting a unique trick of PHP to integrate so closely with HTML form elements
[…] 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 […]
This post helped me a LOT : )
Thank you
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?
Thanks Buddy,
Just got what I am looking for. It helped me a lot. Thanks Again.
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
It’s not working…. my array is not fetching in the next page
Thanx (^___^)
Nice article …working cool..
nice blog thanks for sharing information. keep writing…….. 😉
learn php
Nice and great post this is very useful for us keep blogging. and thanks for share information with us.
php string variables
http://extube.net/content_preview/ero3/amateur/ricons.jpg
нажать и смотреть
скачать прно на мобильный
скaчaть прно нa мобильный
прно скачать на мобильный телефон
скачать прно видео на мобильный
скачать прно ролики для мобильного
мобильные прно фильмы скачать
прно 3gp на мобильный скачать
прно для мобильного скачать торрент
скачать прно снятое на мобильный
скачать прно mp4 на мобильный
скачать прно в мобильном формате
скачать прно игры на мобильный
прно фото для мобильного скачать
скачать прно на мобильный русское
скачать прно картинки на мобильный
скачать частное мобильное прно
скачать прно на мобильный азиатки
короткое прно скачать на мобильный
прно скачать на мобильный онлайн
скачать гей прно на мобильный
скачать прно клипы для мобильного
скaчaть прно нa мобильный
скaчaть прно нa мобильный
скaчaть прно нa мобильный
прно скачать на мобильный телефон
скачать прно видео на мобильный
скачать прно ролики для мобильного
мобильные прно фильмы скачать
прно для мобильного скачать торрент
скачать прно снятое на мобильный
Thanks a lot, it has work form me.
<?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 '
can any one help me how to i get update above query using arry and post,as im new to php….
Alright, totally got it now, thanks raj!
hai
Я знаю где купить новый 5 айфон iphone 5 купить в россии с Доставкой во все города Росси.
Excellent information.Hope more in future.
[…] https://phpprogramming.wordpress.com/2007/05/06/php-passing-array-using-hidden-form-element/ […]