Passing an array using a post form

Discussion in 'PHP' started by al2six, Mar 8, 2008.

  1. #1
    I'm trying to pass an array from one page to another using a form with action=post. however i can't retrieve the array on the 2nd page. I've tried $array=$_POST['array']. I've tried $array[]=$_POST['array[]']. and finally I tried $array[0]=$_POST['array[0]'].

    anyone know how to do this? thanks!
     
    al2six, Mar 8, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    What's your HTML?

    You have to do it like this:
    
    <input type="text" name="foo[]" />
    
    Code (markup):
    Note the square brackets.
     
    nico_swd, Mar 8, 2008 IP
  3. Submerged

    Submerged Active Member

    Messages:
    132
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    I think you're best bet would be to split it out of the array and then put it back together on the other side, assuming it's a simple (one level) array.

    // Page one (stick it in a form):
    for ($round=0;$round<count($array);$round++){
    echo "<input type=hidden name='array$round' value='$array[$round]'>";
    }
    
    //Page two (getting variables)
    for ($round=0;$exists == 0;$round++){
        if (isset($_POST["array$round"])){
        $array[$round] = $_POST["array$round"];
        }
    
        else{
        $exists = 1;
    }
    Code (markup):
    That should do the trick, you might have fix one or two syntax issues as I just type that up without testing. An easier way, if you already have the data when the first page loads, instead of getting it on that page via user input (a form), would be to just use a session.

    //Page 1:
    session_start();
    $_SESSION['array'] = $array;
    
    //Page 2:
    session_start();
    $array = $_SESSION['array'];
    Code (markup):


    A more complete script that will actually run for that:
    //test.php
    <?
    $array[0] = "hi ";
    $array[1] = "there ";
    $array[2] = "mom!";
    
    session_start();
    $_SESSION['array'] = $array;
    ?>
    
    <a href=test2.php>Click!</a>
    
    //test2.php
    <?
    session_start();
    $array = $_SESSION['array'];
    print_r($array); // prints out "hi there mom!"
    ?>
    Code (markup):
    You could use a form submit to go to test2.php, or a meta redirect, or anything you want. I just used a link to make it easier to make.

    Hope this helps!
    -Submerged
     
    Submerged, Mar 8, 2008 IP
  4. al2six

    al2six Active Member

    Messages:
    65
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #4
    al2six, Mar 8, 2008 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    Using your form, $_POST['phones'] would be an array of all selected phones.
     
    nico_swd, Mar 8, 2008 IP
  6. quicksolutions

    quicksolutions Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    expecting the HTML
    <input name="phones[]" type="checkbox" value="$phoneid">

    in PHP

    $phones = $_POST['phones']
    foreach( $phone as $p)
    {
    if( isset($p) && $p<>'')
    {
    ///// place your code here
    }
    }
     
    quicksolutions, Mar 8, 2008 IP