Web Hosting Talk







View Full Version : [PHP] Passing array as string from one file to the next - please help.


dpny
03-02-2008, 01:51 PM
Ok, I am really stuck on this and been searching on here as well as google for the last 2 hours without any luck.
Here is the situation:

I want to pass a multi selectable checkbox as an array to one file called index.php, so on my form page.. I have a for loop that repeats this process 16 times. The form is processed using GET method.

echo "<input name=\"song[]\" type=\"checkbox\" value=\"".$counter."\">";


Now, the index.php holds the variable so I retrieve it using:

foreach($_GET['song'] as $value) {
echo "Checked: $value\n";
}

Cool, this will print out each value... now I want to pass this entire array into a second page in this manner, on the same page:

echo "<a href=\"2nd_page.php?song=$song\">link</a>

I want the $song to carry out the entire array which was passed into index.php to 2nd_page.php.... how can I do that? I dont really want to print out the $value really, that was to see if it works.

I've tried doing this on index.php
$song = $_GET['song'];
and then I did echo to see what shows, it simply displays "Array" nothing else. How can I do this?

Thanks in advanced.
-DPNY

Harzem
03-02-2008, 01:57 PM
It is NOT secure, but anyway I'll say how:

serialize (http://www.php.net/serialize)

Sillysoft
03-02-2008, 02:51 PM
You can store it in a session:


session_start();
$_SESSION['songs'] = $_POST['songs'];

//Then whenever you get to the page you want to use it at assign it to a variable
$songs = $_SESSION['songs'];

foreach($songs AS $key => $value)
{

//do whatever
}

dpny
03-02-2008, 04:21 PM
store into session seem's like a great idea. I am also passing down other information such as id and some other field. I can store them on session. What if the user came back and submited the form again? Is there a way to destroy the session once I do the loop and get what I wanted?

dpny
03-02-2008, 05:32 PM
Thank you guys.. I got it working through session then I am simply destroying the session. Really appriciate all the help.