How to print an array of $POST

Discussion in 'PHP' started by kelsjc, Oct 3, 2010.

  1. #1
    Hi all


    This is probably the easiest question ever :) , but I am not familiar with arrays , so hope somebody can help me


    User post this checkbox form (array):

    <input type="checkbox" name="type[0][]" value="1" >
    <input type="checkbox" name="type[0][]" value="2" >
    <input type="checkbox" name="type[0][]" value="3" >

    I need to get all checked values and print them ...
    How can I do this?


    I tried the code below, but it only returns me one value

    
    
    $values = array($_POST["type"]);
    
    foreach ($values as $item){
    
         echo $item;
    
    }
    
    
    Code (markup):

    Any help???



    Thanks!!
     
    kelsjc, Oct 3, 2010 IP
  2. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $values = array($_POST["type"][0]);
    
    foreach ($values as $item){
    
         echo $item;
    
    }
    PHP:
    might work

    aXe
     
    axelay, Oct 3, 2010 IP
  3. TellyPost

    TellyPost Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try what axelay posted but for an explanation of why its not working. Your only getting 1 value because it is a multidimensional array. So when you say foreach it is looking at the [0] you have, it looks for type[0], type[1], type[2].....and so on. You only have type[0] so it returns that first 1 and stops.
     
    TellyPost, Oct 3, 2010 IP
  4. Monotoko

    Monotoko Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I believe that this is what you are looking for:
    <?php
    function print_array($array)
    {
    print_r($array);
    }
    Code (markup):
    Then all you need to do is pass $_POST to that function like this:
    print_array($_POST);
     
    Monotoko, Oct 3, 2010 IP
  5. TellyPost

    TellyPost Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That would print everything in $_POST though, if all he has is the check box array then that would work but if there is more form fields its going to print it all, not just the check box array.
     
    TellyPost, Oct 3, 2010 IP
  6. kelsjc

    kelsjc Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    ow, thanks for the great tips
    the axelay solution worked perfectly
    and the print_r is very useful :)
     
    kelsjc, Oct 3, 2010 IP
  7. kelsjc

    kelsjc Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    One last thing:
    I need to use this is a sql query.
    When I place this information into my WHERE condition, I get the last value :(

    How to get them as differents variables? should I use "for" instruction?!

    
    $values = array($_POST["type"][0]);
    
    foreach ($values as $item){
    
         echo $item;
         $where_str = " AND gu.id_item='".$item."' ";
    }
    
    Code (markup):
     
    kelsjc, Oct 3, 2010 IP
  8. Lukaslo

    Lukaslo Peon

    Messages:
    751
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I think your query should be in foreach loop
     
    Lukaslo, Oct 3, 2010 IP
  9. kelsjc

    kelsjc Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hi all

    Finally I got it working!
    I had to use "OR" instead of "AND" :)

    Thanks to all who helped me
     
    kelsjc, Oct 3, 2010 IP