GameConsoleRepairCentreUK
asked on
How to pass form data from php processing page to success page?
I would like to know how to pass form data from a php processing page to a success page. I have the following code for my php processing page:
How can I pass the $orderid to my success page? I only need to pass this one value so something simple would be great! :-P
<?php
$stamp = date("Ymdhis");
$random_id_length = 6;
$rndid = generateRandomString( $random_id_length );
$orderid = $stamp ."-". $rndid;
function generateRandomString($length = 10) {
$characters = '0123456789';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
$repairtitle = $_POST['courierrepairtitle'];
$repairconsole = $_POST['courierrepairconsole'];
$repairprice = $_POST['courierrepairprice'];
$outwardpostage = $_POST['outwardpostage'];
$returnpostage = $_POST['returnpostage'];
$name = $_POST['couriername'];
$email = $_POST['courieremail'];
$homephone = $_POST['courierhomephone'];
$mobilephone = $_POST['couriermobilephone'];
$address1 = $_POST['courieraddress1'];
$address2 = $_POST['courieraddress2'];
$address3 = $_POST['courieraddress3'];
$city = $_POST['couriercity'];
$county = $_POST['couriercounty'];
$postcode = $_POST['courierpostcode'];
$country = $_POST['couriercountry'];
$formcontent=" Order No: $orderid \n \n Repair Title: $repairtitle \n Console: $repairconsole \n Price: $repairprice \n \n Outward Postage: $outwardpostage \n Return Postage: $returnpostage \n \n Name: $name \n Email: $email \n Home Phone: $homephone \n Mobile Phone: $mobilephone \n \n Address1: $address1 \n Address2: $address2 \n Address3: $address3 \n City: $city \n County: $county \n Postcode: $postcode \n Country: $country ";
$recipient = "info@example.co.uk";
$subject = "Order Form";
$mailheader = "From: $email \r\n";
// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($homephone) && !empty($address1) && !empty($city) && !empty($postcode) && !empty($country)){
// Test to see if the mail sends successfully:
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: http://www.example.co.uk/courier-mailer-success.htm");
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
exit;
?>
How can I pass the $orderid to my success page? I only need to pass this one value so something simple would be great! :-P
You can't pass it to a plain HTML, you have to use some kind of scripting to display it in the page.
Add session_start() to the beginning of your file and save the data there. The success page will need to be a .php page (assuming your server doesn't tread ".html" pages as php files).
<?php
session_start();
...
// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($homephone) && !empty($address1) && !empty($city) && !empty($postcode) && !empty($country)){
$_SESSION['POST_DATA']=$_P OST;
// Test to see if the mail sends successfully:
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: http://www.example.co.uk/courier-mailer-success.php");
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
exit;
?>
courier-mailer-success.php
<?php
session_start();
echo '<h1>Thank You</h1>';
echo '<p>Below is a summary of what you ordered:</p>';
foreach($_SESSION['POST_DA TA'] as $k=>$v)
{
echo sprintf('<div>%s: %s</div>', htmlspecialchars($k), htmlspecialchars($v) );
}
?>
<?php
session_start();
...
// Test to see if variables are empty:
if(!empty($name) && !empty($email) && !empty($homephone) && !empty($address1) && !empty($city) && !empty($postcode) && !empty($country)){
$_SESSION['POST_DATA']=$_P
// Test to see if the mail sends successfully:
if(mail($recipient, $subject, $formcontent, $mailheader)){
header("Location: http://www.example.co.uk/courier-mailer-success.php");
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
}else{
header("Location: http://www.example.co.uk/courier-mailer-fail.htm");
}
exit;
?>
courier-mailer-success.php
<?php
session_start();
echo '<h1>Thank You</h1>';
echo '<p>Below is a summary of what you ordered:</p>';
foreach($_SESSION['POST_DA
{
echo sprintf('<div>%s: %s</div>', htmlspecialchars($k), htmlspecialchars($v) );
}
?>
ASKER
@DaveBaldwin - Thanks for the reply! I understand it would have to be passed to another PHP page, I'm just not sure how to do it.
See my previous reply. Essentially you don't "pass it" around. You save it to a session variable (the data remains on the server). Then redirect to the other page. Once you are on the other page, you retrieve what you previously saved.
ASKER
@hielo - Thanks for the help!
I only need the $orderid value - nothing else. Your code gives everything on the form apart from the $orderid (the $orderid isn't on the form page, it's generated on the php processing page).
Also gives an error:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/gamecon/public_html/ courier-ma iler-succe ss.php:113 ) in /home/gamecon/public_html/ courier-ma iler-succe ss.php on line 114
I only need the $orderid value - nothing else. Your code gives everything on the form apart from the $orderid (the $orderid isn't on the form page, it's generated on the php processing page).
Also gives an error:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/gamecon/public_html/
Just a question - is there any reason you can't just post directly to the final page - is there a need for it to go via the intermediary?
You could probably achieve what you want with include statements
You could probably achieve what you want with include statements
<?php
// Assigment here
// mail here
if(mail($recipient, $subject, $formcontent, $mailheader)){
require_once("success_page.php");
else {
require_once("failure_page.php");
}
//etc
That way you don't have to do any redirects because all the variables you created at the start of the script will be visible to the included scripts.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks that's great