Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing array through url

Status
Not open for further replies.

sophielois

Technical User
Sep 8, 2005
66
GB
Is it possible to pass an array through a url?

I cant get it to work...

this is what im doing
Code:
<?
$units = $_POST['cb']; /* array which chosen units are stored */
?>
<?
			echo "<td colspan='2'><font face='Verdana, Arial, Helvetica, sans-serif'><b>Personal Details</b> &nbsp;&nbsp;&nbsp;- <a href='/admin/editpersonal.php?userid=$userid&units=$units'>Edit Personal Details</a></font></td>";
?>


At the mo this is not working, when i display $units it prints Array

Never tried to do this before so unsure if it is even possible.

Sophx
 
Are you using the foreach loop that I posted in your other post? If you just use 'echo $units' the output you'll get is 'Array'. This is php telling you that what you are trying to print out is an array and can't be accesed in this way. You have to loop through the Array and access $units[0], $units[1] etc..
 
hi dweezel,

I tried using

Code:
foreach($units as $result)
 {
   echo "$result</br>";
 }

but it returned

Code:
Warning: Invalid argument supplied for foreach() in /home/n/v/nvqwand/public_html/admin/editpersonal.php on line 30

i also tried echo " $units[0]";

and it printed A

Sophx
 
Write it like this:

Code:
<?
$units = $_POST['cb']; /* array which chosen units are stored */
?>
<?
            echo "<td colspan='2'><font face='Verdana, Arial, Helvetica, sans-serif'><b>Personal Details</b> &nbsp;&nbsp;&nbsp;- <a href='/admin/editpersonal.php?userid=".$userid."&amp;".units=".$units."'>Edit Personal Details</a></font></td>";
?>


 
Sorry, another typo
Code:
<?
$units = $_POST['cb']; /* array which chosen units are stored */
?>
<?
            echo "<td colspan='2'><font face='Verdana, Arial, Helvetica, sans-serif'><b>Personal Details</b> &nbsp;&nbsp;&nbsp;- <a href='/admin/editpersonal.php?userid=".$userid."&amp;units=".$units."'>Edit Personal Details</a></font></td>";
?>
 
hi dweezel,

im still getting an error with this, cant work it out

Code:
Warning: Invalid argument supplied for foreach() in /home/n/v/nvqwandp/public_html/55731admin/editpersonal.php on line 30

this is the code im using
Code:
$units = $_POST['cb']; /* array which chosen units are stored */

echo "<td colspan='2'><font face='Verdana, Arial, Helvetica, sans-serif'><b>Personal Details</b> &nbsp;&nbsp;&nbsp;- <a href='/admin/editpersonal.php?userid=".$userid."&amp;units=".$units."'>Edit Personal Details</a></font></td>";

Code:
$units = $_GET["units"]; /* array which chosen units are stored */

foreach($units as $result)
 {
   echo "$result</br>";
 }



 
sorry ammend

Code:
$units = $_POST['cb']; /* array which chosen units are stored */

echo "<td colspan='2'><font face='Verdana, Arial, Helvetica, sans-serif'><b>Personal Details</b> &nbsp;&nbsp;&nbsp;- <a href='/55731admin/editpersonal.php?userid=".$userid."&amp;units=".$units."'>Edit Personal Details</a></font></td>";

really appreciate you help you've given me

Thanks
Sophx
 
Sorry Sophie. I was answering your question in the early hours of the morning after far too many beers and I'm afraid my previous answers were completely misleading. Sorry.

You can't pass an array this way. What your code above will do is try and echo the code to the URL string, and as it is an array, that is not possible.

You can pass array variables forward in a php script in a form element or in a session_variable but not on a URL.

 
ahh ok thanks Dweezel, i was unsure if it was possible.

Thanks for all your help, ill sort out some kind of work around.

Thanks again

Sophxx
 
stick this in there and post the output please:

Code:
$units = $_POST['cb']; /* array which chosen units are stored */

echo "<pre>";
print_r($units);
echo "</pre>";

die();
 
To use sessions to pass the value forward:

put this at the top of both the page you are sending to and the page you are sending from:


Code:
<?php
session_start();
?>

beneath this on the page you are sending the array from use:

Code:
$your_array = $_POST['cb']; 
$_SESSION['units'] = $your_array;


When you click the link between the two pages,your array will now be available in the second page as $units. No need to include the $units variable in the url.

Sorry about last night. I really should stay away from my computer when I've been to the pub.
 
no worries dweezel,

when i get this done its the first place ill be heading

Thanks for the information, ive got some knowledge of sessions and/or post so should be able to complete it

Thank you

Sophxx
 
Dweezel said:
I really should stay away from my computer when I've been to the pub.

been there. there's been many emails I wish I never sent after a trip to the bar! :)
 
Hi again,

im still having trouble with this, this time using post.

page1
Code:
$units = $_POST['cb']; /* array which chosen units are stored */

echo "<td><form action='editpersonal.php' method='post'><input type='hidden' name='userid' value='$userid'><input type='hidden' name='award' value='$award'><input type='hidden' name='units' value='$units'><input type='submit' name='Update' value='Update Personal Information'></form></td>";

page2
Code:
$units = $_POST['units']; /* array which chosen units are stored */

foreach($units as $result)
echo "<pre>";
print_r($units);
echo "</pre>";

this gives the following error
Code:
Warning: Invalid argument supplied for foreach() in /home/n/v/nvqwandp/public_html/55731admin/editpersonal.php on line 29
Array


ive also tried the following on page 2
Code:
foreach($units as $result)
 {
   echo "$result</br>";
 }

This also gets an error.


I can display the array on page one but not on page 2??

I hope you can help me again

Thankyou

Sophx
 
Ok, here are some explanations:

Page 1 receives POST variables. Therefore $units is an array. However, you can't just 'print' or 'echo' an array into a hidden field. Hidden fields are just plain text fields.

In order to transfer an array that way you need to serialize it before printing it into the hidden field. On page 2 you unserialize the value of the plain text field to reestablish the array.

If your array also contains some kind of quotes additional handling to make the quotes not disturb the HTML syntax is necessary.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top