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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass an Array in hidden fields through form post

Status
Not open for further replies.

sallieann

Programmer
Sep 2, 2003
28
GB
Can anybody help – I’ve been going over this problem for days and can’t find a solution! I am trying to pass the values from one page to another in hidden values. All of my values are being echoed properly on the second page except those that are in the array. The values in the array are just being echoed as the word “Array”. As I will be eventually passing the value into another system I cannot declare any of the variables on the second page, but if do echo them, then the code works correctly (so I know the code is correct). Does anybody know if I need to add some more code for passing array variables through form fields?

My code is as follows;

Code:
echo"<form target=\"_self\" action=\"test.php\" method=POST>";

$OKCode = "ADDI";
$TARGET = "_top";
$CALLER = "CTLG";

	$sql_select = mysql_query( "select * from ".$prefix."store_customer, store_company where store_customer.company_id=store_company.company_id and email='$email'");

    while ($row = mysql_fetch_array($sql_select)){
	
$EMAIL =$row["email"];
$PASSWORD =$row["password"];
}

  $contents = $cart->display_contents($prefix,$temp_session,$sale);
  if($contents[product][0] !=  "")
  {

    $i = 0;
	while($i != $cart->num_items($prefix,$temp_session))
    {
	   $CODE  = $contents[CODE][$i];
	   $CURRENCY  = $contents[currency][$i];
	   $UNIT  = $contents[uom][$i];
	   $PRICE  = $contents[price][$i];	   
       $CARTTOTAL = $contents[total][$i];
       $QUANTITY  = $contents[quantity][$i];
	   
	   $i ++;
	
PRINT "
<input type=\"hidden\" size=\"100\" name=\"EMAIL\" value=\"$EMAIL\">
<input type=\"hidden\" size=\"100\" name=\"PASSWORD\" value=\"$PASSWORD\">
<input type=\"hidden\" size=\"100\" name=\"CODE[$i]\" value=\"$CODE\">
<input type=\"hidden\" size=\"100\" name=\"QUANTITY[$i]\" value=\"$QUANTITY\">
<input type=\"hidden\" size=\"100\" name=\"PRICE[$i]\" value=\"$PRICE\">
<input type=\"hidden\" size=\"100\" name=\"UNIT[$i]\" value=\"$UNIT\">
<input type=\"hidden\" size=\"100\" name=\"CURRENCY[$i]\" value=\"$CURRENCY\">
<input type=\"hidden\" size=\"100\" name=\"CARTTOTAL[$i]\" value=\"$CARTTOTAL\">
<input type=\"hidden\" size=\"100\" name=\"OKCode\" value=\"$OKCode\">
<input type=\"hidden\" size=\"100\" name=\"TARGET\" value=\"$TARGET\">
<input type=\"hidden\" size=\"100\" name=\"CALLER\" value=\"$CALLER\">";

echo"</form>";
}

}
 
thats because when u do:
echo ArrayName;

rather than priting the contents of the array it will print only array. u have to go through all the contetns of the array using a loop and write it to the hidden field...

Known is handfull, Unknown is worldfull
 
Thanks for you help, but what I am trying to work out is as follows;

Would my code correctly pass all of the form variables to the second page (because the receiving page doesn't seem to be able to echo them to the screen). If it is only a problem with my second page then this is fine because this is my test page and the form will actually be passing the values to an external system. Because the second page isn't echoing the variables I need to know if they are being passed accross at all.

ie, is this how you pass variables across using PHP?
 
I'll be honest, I didn't read the whole first post... but I'll zero in on your
ie, is this how you pass variables across using PHP?

And say no, probably not.

IMO, you have two real options... the first, and most likely the best, is to use sessions, you can search for session_start() in the php manual and go to town from there.

The second is to use the serialize function, put the output from that into a variable on the page, then use the unserialize function to retrieve it.
 
>>Would my code correctly pass all of the form variables to the second page

No. like i said the form page shoud use a loop...

Known is handfull, Unknown is worldfull
 
Thanks for your help, I am managing to pass the values accross now using sessions.

How would I loop through session variables to show an array?

Code:
<form target=\"_self\" action=\"test.php\" method=POST>

$_SESSION['MATGROUP'] = $MATGROUP;

<input type=\"hidden\" name=\"MATGROUP\" value=\"$MATGROUP\">

</form>
 
Using sessions you can now trash the hidden field.

Now on the receiving page $_SESSION['MATGROUP'] looks just like $MATGROUP did before.

So you can either do
$MATGROUP=$_SESSION['MATGROUP'];
and access it just like you used to, or you can work directly on the $_SESSION variable itself.... i.e.
Code:
foreach ($_SESSION['MATGROUP'] as $key=>$value)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top