I have an array and I need to pass it to another script. Can I do this with a form? I know I can do variables in a <input type="hidden"... tag... but how do I pass an array?

Whenever I try, it just says text: "Array"

Any slick ways? Or do I have to break down the array, pass it in chunks, and reconstruct it in the next script?

    if (!isset($_POST['submit']))
    {
    	$array = array(
    		'one',
    		'two',
    		'three',
    		'four',
    		'five'
    	);
    
    echo '
    <form action="" method="POST">
    this form has a hidden array<br>
    <input type="hidden" name="array" value="' . htmlspecialchars(serialize($array)) . '"> 
    <input type="submit" name="submit" value="submit">
    </form>
    ';
    }
    else
    {
    	$array = unserialize(stripslashes($_POST['array']));
    	print_r($array);
    }
    

      Damn, PHP has some great tricks built in eh? I just wrote this same little thing for Java. It only took 5 minutes but still, having it built in is great 🙂

        htmlspecialchars() is fine, but base64_encode() would probably be better as it'd reduce the string to alphanumerics (no special chars needing to be converted)

        <input type="hidden" value="<?php echo base64encode(serialize($array)); ?>" ... />
        

          Just remember that if you do send something out to the client and expect it to come back in a form, that whoever is visiting your site might have messed with it. You can't trust what you get back.

          If you're just sending this data back to yourself you'd probably be better of just using a session to store the data in.

          "What's this hidden field? dG90YWwgY29zdDokNjE1Ljk1? I know, I'll change that to dG90YWwgY29zdDokMTUuOTU=! Sweet!"

            FYI (in case anyone refers back to this thread) the method name is base64_encode

              Write a Reply...