Link to home
Create AccountLog in
Avatar of GameConsoleRepairCentreUK
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:

<?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;
?>

Open in new window


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
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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']=$_POST;
    // 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_DATA'] as $k=>$v)
{
      echo sprintf('<div>%s: %s</div>', htmlspecialchars($k), htmlspecialchars($v) );
}
?>
Avatar of GameConsoleRepairCentreUK
GameConsoleRepairCentreUK

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.
@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-mailer-success.php:113) in /home/gamecon/public_html/courier-mailer-success.php on line 114
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
<?php

// Assigment here

// mail here

if(mail($recipient, $subject, $formcontent, $mailheader)){
   require_once("success_page.php");
else {
   require_once("failure_page.php");
}
//etc

Open in new window

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
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks that's great