By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,888 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,888 IT Pros & Developers. It's quick & easy.

get hidden array values

P: 45
Hi
i have a array values in hidden format
[HTML]<form name="form2" method="post" action="test.php">
<input type="hidden" name="date_val[]" value="<%= date[0] %>">
<input type="hidden" name="name_val[]" value="<%= name[0] %>"></form>[/HTML]

my question is how can i get the array values in php using array(). In foreach statement i got the result but i want to do it using array to retrive the values one by one. i want to store the hidden values in table format like this

Expand|Select|Wrap|Line Numbers
  1. date                 name  
  2.  
  3. 10-10-2008        xxxx
  4. 15-09-2008        yyyy
  5.  
  6.  
in this format i want to print the array values. please any one help to solve this.

Thanks and Regards
Geethu
Oct 20 '08 #1
Share this Question
Share on Google+
5 Replies


Atli
Expert 5K+
P: 5,058
Hi.

When you receive data from a form that looks like this:
Expand|Select|Wrap|Line Numbers
  1. <form action="?" method="post">
  2.   <input type="hidden" name="data[]" value="1" />
  3.   <input type="hidden" name="data[]" value="2" />
  4.   <input type="hidden" name="data[]" value="3" />
  5. </form>
  6.  
An array will be created in the $_POST super-global that would be identical to an array created like so:
Expand|Select|Wrap|Line Numbers
  1. $_POST['data'] = array('1', '2', '3');
  2.  
You could iterate through it like so:
Expand|Select|Wrap|Line Numbers
  1. foreach($_POST['data'] as $_key => $_value) {
  2.   echo "Data #{$_key} = {$_value}";
  3. }
  4.  
Which would print:
Expand|Select|Wrap|Line Numbers
  1. Data #0 = 1
  2. Data #1 = 2
  3. Data #2 = 3
  4.  
Does this answer you question?

P.S.
I notice that you use ASP-like tags. This is highly discouraged, because this feature is not enabled by default, and will be removed in PHP6. It is almost guaranteed to cause problems if you ever plan on upgrading or switching servers.

I recommend getting used to using the standard tags. They will work no matter what.
That is, instead of doing: <%= $var %>
Do: <?php echo $var; ?>
Oct 20 '08 #2

P: 45
Hi.

When you receive data from a form that looks like this:
Expand|Select|Wrap|Line Numbers
  1. <form action="?" method="post">
  2.   <input type="hidden" name="data[]" value="1" />
  3.   <input type="hidden" name="data[]" value="2" />
  4.   <input type="hidden" name="data[]" value="3" />
  5. </form>
  6.  
An array will be created in the $_POST super-global that would be identical to an array created like so:
Expand|Select|Wrap|Line Numbers
  1. $_POST['data'] = array('1', '2', '3');
  2.  
You could iterate through it like so:
Expand|Select|Wrap|Line Numbers
  1. foreach($_POST['data'] as $_key => $_value) {
  2.   echo "Data #{$_key} = {$_value}";
  3. }
  4.  
Which would print:
Expand|Select|Wrap|Line Numbers
  1. Data #0 = 1
  2. Data #1 = 2
  3. Data #2 = 3
  4.  
Does this answer you question?

P.S.
I notice that you use ASP-like tags. This is highly discouraged, because this feature is not enabled by default, and will be removed in PHP6. It is almost guaranteed to cause problems if you ever plan on upgrading or switching servers.

I recommend getting used to using the standard tags. They will work no matter what.
That is, instead of doing: <%= $var %>
Do: <?php echo $var; ?>

thank you for ur reply.

i am not understand how can i get the value as 123 ie($_POST['data'] = array('1', '2', '3');) i don't know what value is in the data. i am using a jsp tags to get the array value for data field. if i know the values of data your solution is correct but i don't know the value of data[] field. please give me little bit clear.

thanks
Oct 20 '08 #3

Atli
Expert 5K+
P: 5,058
You don't have to define the data element in your $_POST super-global yourself.
PHP will do that automatically when you post a form like the one I showed.

Any <input> element in the form will be automatically added to the $_POST array. If a number of <input> elements share the same name, and that name ends with [], then all of those elements will be added as an array into the same element.

That is all there is to it.
What you do with this data once PHP has added it to the $_POST array is up to you.

If this isn't making sense, try printing the entire $_POST array after submitting your form. Maybe that will help you understand:
Expand|Select|Wrap|Line Numbers
  1.  echo "<pre>", print_r($_POST, true), "</pre>";
Oct 20 '08 #4

P: 45
You don't have to define the data element in your $_POST super-global yourself.
PHP will do that automatically when you post a form like the one I showed.

Any <input> element in the form will be automatically added to the $_POST array. If a number of <input> elements share the same name, and that name ends with [], then all of those elements will be added as an array into the same element.

That is all there is to it.
What you do with this data once PHP has added it to the $_POST array is up to you.

If this isn't making sense, try printing the entire $_POST array after submitting your form. Maybe that will help you understand:
Expand|Select|Wrap|Line Numbers
  1.  echo "<pre>", print_r($_POST, true), "</pre>";
Thank you very much.

i got the result. but the result is print in report format like one by one. i am using a code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. $date[i] = print_r($_POST['Date'], true);       
  3.     echo "<td>".$date[i]."</td>";      
  4.  $name[i] = print_r($_POST['Name'], true);      
  5.     echo "<td>".$name[i]."</td>";
  6.  
in the above coding i got the result like this.
Expand|Select|Wrap|Line Numbers
  1.   Date                                                                          Name
  2.  Array ( [0] => 2008-08-29 [1] =>                  Array ( [0] => x [1] => y [2] => z)
  3.  2008-08-30 [2] =>
  4.  2008-09-07 [3] => 2008-09-08 [4] =>
  5.  2008-09-26 )                                          
  6.  
i want to print in the below format

Expand|Select|Wrap|Line Numbers
  1.   Date                                              Name
  2.  2008-08-29                                       x
  3.  2008-08-30                                       y
  4.  2008-09-07                                       z
  5.  2008-09-08                                       a
  6.  2008-09-26                                       b             
  7.  
pls give me your idea....

Thanks
Oct 21 '08 #5

Atli
Expert 5K+
P: 5,058
The print_r function is of no use in this regard. It just creates a nicely formatted view of the array for debugging purposes.

To do this, you will have to loop through the results, using for or foreach, and print the appropriate elements from the arrays.

Like:
Expand|Select|Wrap|Line Numbers
  1. echo "Date\tName";
  2. for($i = 0; $i < count($_POST['Date']); $i++) {
  3.   echo $_POST['Date'][$i] . "\t\. $_POST['Name'][$i];
  4. }
  5.  
This assumes that the Name array has an element for each element in the Date array. You may want to make sure this is true before executing this.
Oct 21 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.