get hidden array values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Geethu03
    New Member
    • Dec 2006
    • 45

    #1

    get hidden array values

    Hi
    i have a array values in hidden format
    [HTML]<form name="form2" method="post" action="test.ph p">
    <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

    Code:
    date                 name  
    
    10-10-2008        xxxx
    15-09-2008        yyyy
    in this format i want to print the array values. please any one help to solve this.

    Thanks and Regards
    Geethu
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    When you receive data from a form that looks like this:
    [code=html]
    <form action="?" method="post">
    <input type="hidden" name="data[]" value="1" />
    <input type="hidden" name="data[]" value="2" />
    <input type="hidden" name="data[]" value="3" />
    </form>
    [/code]
    An array will be created in the $_POST super-global that would be identical to an array created like so:
    [code=php]
    $_POST['data'] = array('1', '2', '3');
    [/code]
    You could iterate through it like so:
    [code=php]
    foreach($_POST['data'] as $_key => $_value) {
    echo "Data #{$_key} = {$_value}";
    }
    [/code]
    Which would print:
    Code:
    Data #0 = 1
    Data #1 = 2
    Data #2 = 3
    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; ?>

    Comment

    • Geethu03
      New Member
      • Dec 2006
      • 45

      #3
      Originally posted by Atli
      Hi.

      When you receive data from a form that looks like this:
      [code=html]
      <form action="?" method="post">
      <input type="hidden" name="data[]" value="1" />
      <input type="hidden" name="data[]" value="2" />
      <input type="hidden" name="data[]" value="3" />
      </form>
      [/code]
      An array will be created in the $_POST super-global that would be identical to an array created like so:
      [code=php]
      $_POST['data'] = array('1', '2', '3');
      [/code]
      You could iterate through it like so:
      [code=php]
      foreach($_POST['data'] as $_key => $_value) {
      echo "Data #{$_key} = {$_value}";
      }
      [/code]
      Which would print:
      Code:
      Data #0 = 1
      Data #1 = 2
      Data #2 = 3
      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

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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:
        [code=php] echo "<pre>", print_r($_POST, true), "</pre>";[/code]

        Comment

        • Geethu03
          New Member
          • Dec 2006
          • 45

          #5
          Originally posted by Atli
          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:
          [code=php] echo "<pre>", print_r($_POST, true), "</pre>";[/code]
          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.
          Code:
           
          $date[i] = print_r($_POST['Date'], true);  	 
          	echo "<td>".$date[i]."</td>";  	
           $name[i] = print_r($_POST['Name'], true);  	
          	echo "<td>".$name[i]."</td>";
          in the above coding i got the result like this.
          Code:
            Date	                                                                      Name
           Array ( [0] => 2008-08-29 [1] =>                  Array ( [0] => x [1] => y [2] => z)
           2008-08-30 [2] =>
           2008-09-07 [3] => 2008-09-08 [4] =>
           2008-09-26 )
          i want to print in the below format

          Code:
            Date	                                          Name
           2008-08-29                                       x
           2008-08-30                                       y
           2008-09-07                                       z
           2008-09-08                                       a
           2008-09-26                                       b
          pls give me your idea....

          Thanks

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            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:
            [code=php]
            echo "Date\tName ";
            for($i = 0; $i < count($_POST['Date']); $i++) {
            echo $_POST['Date'][$i] . "\t\. $_POST['Name'][$i];
            }
            [/code]
            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.

            Comment

            Working...