Passing variables using GET

Discussion in 'PHP' started by enchance, Oct 8, 2007.

  1. #1
    How do I pass a small array using GET?
     
    enchance, Oct 8, 2007 IP
  2. xemiterx

    xemiterx Peon

    Messages:
    62
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    That is what serialize() is for.

    $my_array[0] = 'hello';
    $my_serialized_array = serialize($my_array);
    header ("Location: http://www.url.ext/file.php?array=$my_serialized_array");
    PHP:
     
    xemiterx, Oct 8, 2007 IP
  3. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So serialize() is like a zip file only it contains variables...nice! So it's gonna be like this?

    //in page 1
    $myarray[] = "Sonic";
    $myarray[] = "Mario";
    $myarray[] = "Linus Torvalds";
    $myarray[] = "Contra";
    $serializeme = serialize($myarray);
    header ("Location: http://www.url.ext/file.php?getme=$serializeme");
    
    //passes the array to page 2
    $gotit = $_GET['getme'];
    $var = unserialize($gotit);
    echo count($var); //should return 4
    PHP:
    Right?
     
    enchance, Oct 9, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    serialize() is not meant for that, even if it might be possible. (urlencode() would still be necessary)

    You can pass arrays via URL like this.

    
    header ("Location: http://www.url.ext/file.php?getme[]=Sonic&getme[]=Mario&getme[]=Linus");
    
    PHP:
    You can also give the array specific keys.
    
    header ("Location: http://www.url.ext/file.php?getme[foo]=Sonic&getme[foo]=Mario&getme[bar]=Linus");
    
    PHP:
     
    nico_swd, Oct 9, 2007 IP
  5. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I get it. I have another question, how would a hidden form work with this? I've never used one before so in short, I have no idea what I'm talking about. hehehe... is it even related to this post? :p
     
    enchance, Oct 9, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Same as normal fields. Hidden fields are ONLY hidden in the HTML form.. for the user's view. You cannot hide them in the URL. But you could use POST method instead.
     
    nico_swd, Oct 9, 2007 IP
  7. enchance

    enchance Peon

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the info nico_swd! It's much appreciated!
     
    enchance, Oct 9, 2007 IP
  8. gdowkpc

    gdowkpc Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm not understanding how to send the array to another page.

    My first page has a form that sends an array (named $table) of states to a results page. The array should look like this:

    [0] => AKrpt
    [1] => AZrpt
    [2] => CArpt
    [3] => IDrpt
    [4] => ORrpt


    The results are printed in text format, but there is a link to map the results.

    A new query is run on this third page to map the results, but the query needs the array passed to it from the second page, preferably in the link. During the testing process, I have just been sending one option, so an array with only one key, for simplicity.

    I have been searching how to pass an array through a url and see there are issues with it. I have seen where sessions are recommended. I haven't been able to see an example of it working.

    Here is some of my code:

    This is the link on the second page to page 3:

    $table = $_REQUEST['table'];
    $table_serialized = serialize($table);
    ?>
    <? echo "<li><a href=\"http://nwham.com/repeaters/map_multi.php?table=$table_serialized\" target='_top'\">Map</a></li>\n"; ?>
    PHP:
    On the third page I have tried to unserialize the array:

    $table = $_REQUEST['table'];
    $table = unserialize($serialized_table);
    PHP:
    But the link looks like this: http://nwham.com/repeaters/map_multi.php?table=a:1:{i:0;s:5:

    and of course, fails.
     
    gdowkpc, Nov 4, 2007 IP
  9. gdowkpc

    gdowkpc Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I am trying it like this, too

    <? echo "<li><a href=\"http://nwham.com/repeaters/map_multi.php?table=urlencode($table)\" target='_top'\">Map</a></li>\n"; ?>
    
    PHP:
    But the URL looks like this:

    http://nwham.com/repeaters/map_multi.php?table=urlencode(Array)

    Which probably means I have no idea what I am doing or how to do it? :(:confused:
     
    gdowkpc, Nov 4, 2007 IP
  10. gdowkpc

    gdowkpc Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I just need a simple example of how to encode() the array into a url. I am now trying:

    <? $table = urlencode($table) ?>
    <? echo "<li><a href=\"http://nwham.com/repeaters/map_multi.php?table=$table\" target='_top'\">Map</a></li>\n"; ?>
    PHP:
    and am getting:

    Warning: urlencode() expects parameter 1 to be string, array given in....


    Can someone just show me a quick example to pass the array?
     
    gdowkpc, Nov 4, 2007 IP