Click to See Complete Forum and Search --> : Passing an array


sc_king
08-05-2008, 06:36 PM
What is the best way to pass an array of information to the next page?

I have a payment page which deals with the processing and receives the output. I want to pass the information received to the receipt page.

I've tried forwarding to the receipt using header with a serialized array. Not sure if this is a good idea.

Sheldon
08-05-2008, 08:55 PM
Set the array as a session.


<?php
// start the session at the top of the page.
session_start();

// construct your array
$array = array("value1" => "$5.00","Product2"=>"$4.95");

$_SESSION['array'] = $array;

header("Location: next_page.php");

?>
Then on your next_page.php


<?php
// start the session at the top of the page.
session_start();


foreach($_SESSION['array'] as $product => $cost){
echo($product ." - ". $cost);
}

?>

sc_king
08-05-2008, 09:22 PM
Oh Awesome, Thanks!

I never knew you could do that.

maveruk
08-06-2008, 03:47 AM
Give this a try instead.

header("POST next_page.php HTTP/1.0");

That should post all POST on your current page to your next page.

sc_king
08-06-2008, 08:49 AM
Thanks again!,

I have another question, how do people output a transaction process on the payment page and then forward to the receipt page?
Using a header while doing any output will create a header error.

maveruk
08-06-2008, 08:56 AM
Thanks again!,

I have another question, how do people output a transaction process on the payment page and then forward to the receipt page?
Using a header while doing any output will create a header error.

Yeah, the downside is that the header has to be called before you display anything on your page, so if you want a page before you POST the data to a new page, you can't.


If the POST information is being stored in a database, you could pass a unique key along to the next page, possibly with an encryption key like md5(md5("foobar" . $uniqueid))

Then you can have a "next" button on your page in a form targetting the next page, and have the encrypted unique key and normal unique key in 2 hidden fields, which brings the data up on the next page from the database.

Do you follow what I mean?

sc_king
08-06-2008, 09:57 AM
Hi Maveruk

Thanks for the quick reply.

I get what you mean, but how do certain business have it?
Our current payment gateway has a process bar during the payment phase, and it when it is complete it automatically forwards to the receipt page. It doesn't have any buttons either.

Not sure how that is done.

Thanks again.

SyCo
08-06-2008, 10:21 AM
If you want to output a message then header() is out. You could meta refresh but you would have to set an arbitrary time to move to the receipt page so that's not very good. The other ways of doing it rely on javascript. One way uses ajax. This is a way to do it with Xajax (http://xajaxproject.org) Xajax is a very cool class for PHP coders with rusty javascript. The ajax way allows you do a lot more if necessary, like print a message to the page with the state of the transaction etc.


<?
require("xajax.inc.php");
$xajax = new xajax();

function lookup() {
$objResponse = new xajaxResponse();
//process the transaction here you can use all and any PHP
for($i=0;$i<10000000;$i++){
// delay loop
rand(0,100000);
}
$objResponse->addscript('document.location.href ="http://www.google.com"');
return $objResponse;
}
$xajax->registerFunction("lookup");
$xajax->processRequests();

?>
<html>
<head>
<?
$xajax->printJavascript();
?>
</head>
<body>
Processing transaction please wait...
<script type="text/javascript">
<!--
xajax_lookup();
//-->
</script>
</body>
</html>


and this is a way to do it with an invisible iframe

parent page

</head>
<body>
Processing transaction please wait
<iframe
src ="iframe.php"
width="0" height="0">
</iframe>
</body>
</html>

and the source of the iframe

<?
//process transaction here
for($i=0;$i<10000000;$i++){
// delay loop
rand(0,100000);
}
?>
<html>
<head>
</head>
<body>
<script type="text/javascript">
<!--
parent.location.href ="http://www.google.com"
//-->
</script>
</body>
</html>


With both these methods you can be sure the page will redirect to the receipt only after transaction is complete. In both examples there's a delay loop to simulate the time it takes to perform the transaction.

maveruk
08-06-2008, 10:25 AM
I was about to suggest ajax, but I was beaten to it, it seems.