Passing an entire array in PHP

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default Passing an entire array in PHP

    Hi,

    I saw a previous post about sending arrays but did not quite
    understand the answers.

    The problem is that I would like to pass an entire array as a hidden
    input field from one php script to another. I've simplified the code
    where form1.php calls form2.php when "Go" is hit:

    form1.php
    =========

    <html>
    <head>
    <title>Form 1</title>
    </head>
    <body>
    <h1>Form 1</h1>
    <form action="form2.php" method=post>
    <?
    $record[0]="One";
    $record[1]="Two";
    printf("<input type=hidden name=\"record[]\" value=\"%s\">",$record);
    ?>
    <input type=submit value="Go">
    </form>
    </body>
    </html>

    form2.php
    =========
    <html>
    <head>
    <title>Form 2</title>
    </head>
    <body>
    <h1>Form 2</h1>
    <?
    $record=$_POST['record'];
    printf("$record[0]=%s",$record[0]);
    printf("$record[1]=%s",$record[1]);
    ?>
    </body>
    </html>

    The result is:
    Form 2
    Array=Array=

    How do I fix this?

    Thanks in advance for any help.
    Phillip Wu Guest

  2. Similar Questions and Discussions

    1. Passing value to Array
      If I've got x = "test5" How can I put the value of x to Array list=; I'd try to use ---> list= but is not work.
    2. passing array in the url
      Hello there, I am unable to pass an array thought he url. Even the most simple array as in the code: $test = array(1,2,3,4); echo "<a...
    3. passing an array
      Hello, what is the best way to pass an array to a sub routine, IE. my @fields = qw(one two three); send_array(@fields);
    4. Passing Array Via CGI.pm
      Hello all, I'm a Perl newbie here and have a quick question regarding CGI.pm. I have a CGI script that passes an array to another CGI script as...
    5. Dumping an entire array to the screen
      for iCounter = 0 to ubound(TheArray) response.write TheArray(iCounter) & "<br />" next Ray at work "Jacob" <yak2016@comcast.net> wrote in...
  3. #2

    Default Re: Passing an entire array in PHP

    Phillip Wu wrote:
    > Hi,
    <snip>
    rtfm
    but in brief:
    echo '<input type="hidden" name="myarray" value="',
    base64_encode(serialize($myarray)),'">';

    to get your array back:
    $thearray = unserialize(base64_decode($_POST['myarray']));


    Agelmar Guest

  4. #3

    Default Re: Passing an entire array in PHP

    If you want to pass an array from one PHP script to another then you
    would be better off using sessions. The disadvantage of passing data
    as hidden fields in your HTML form is that anybody can see it using
    the browser's 'view source' option. It is even possible to change the
    values before hitting the 'submit' button.

    Learn how to use sessions. You won't be sorry you did.

    Tony Marston


    [email]pwu@qantas.com.au[/email] (Phillip Wu) wrote in message news:<943cec75.0307141810.488a33d4@posting.google. com>...
    > Hi,
    >
    > I saw a previous post about sending arrays but did not quite
    > understand the answers.
    >
    > The problem is that I would like to pass an entire array as a hidden
    > input field from one php script to another. I've simplified the code
    > where form1.php calls form2.php when "Go" is hit:
    >
    > form1.php
    > =========
    >
    > <html>
    > <head>
    > <title>Form 1</title>
    > </head>
    > <body>
    > <h1>Form 1</h1>
    > <form action="form2.php" method=post>
    > <?
    > $record[0]="One";
    > $record[1]="Two";
    > printf("<input type=hidden name=\"record[]\" value=\"%s\">",$record);
    > ?>
    > <input type=submit value="Go">
    > </form>
    > </body>
    > </html>
    >
    > form2.php
    > =========
    > <html>
    > <head>
    > <title>Form 2</title>
    > </head>
    > <body>
    > <h1>Form 2</h1>
    > <?
    > $record=$_POST['record'];
    > printf("$record[0]=%s",$record[0]);
    > printf("$record[1]=%s",$record[1]);
    > ?>
    > </body>
    > </html>
    >
    > The result is:
    > Form 2
    > Array=Array=
    >
    > How do I fix this?
    >
    > Thanks in advance for any help.
    Tony Marston Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139