I have a form that customers fill in with personal data and also makes choices regarding the service. So with the $_GET function I retrieve the information of that form, do some basic math and show all the information so the customer can preview the order and then hit “SEND” to confirm the job.
I need to send that Preview Order to me by mail but I don’t know how to send all that. I know how to retrieve data from a form and then send it, but have no idea how to send those variables.
FORM
// I am omitting elements to make this shorter
<select name="Amount">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
PREVIEW ORDER
$price = 5;
// I am omitting elements to make this shorter
// here I do some math
$Amount = $_GET['Amount'];
// check value and select appropriate item
if ($Amount== "1") {
$extra = "1";
}
elseif ($Amount == "2") {
$extra = "2";
}
elseif ($Amount == "3") {
$extra = "3";
// here is the order preview and this is what I need to email to myself
// the customer should look this preview and then HIT a confirm buttom to get this sent
<?php echo $Name;?><br>
<?php echo $Address;?><br>
<?php echo $E-mail;?><br>
<?php echo $Phone;?><br>
Your order: <?php echo $extra . " " . "products, for a total of" . " " . ($price * $extra); ?>
——————– FINAL VERSION ————— THANKS to all of you guys!
I’ll be only using 2 fields in order to shrink the code
// FORM — The user entries his data
<form method="get" id="order" action="order-info.php">
<h1>Personal Info</h1>
<p>name: <input name="name" type="text" /></p>
<p>email: <input name="surname" type="text" /></p>
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Continue">
</form>
//ORDER Preview — Here the user can preview all of his data and product related choices before confirming the order
<?php session_start(); ?>
<?php
// get info personal
$Name = $_GET['name'];
$Email = $_GET['email'];
?>
// Now I echo the info
<h2><?php echo $Name . " " . $Email ; ?><br></h2>
<?php
// here's where the magic is done thanks to Sheldon Ferns!
$_SESSION['customerInfo']['name'] = $Name;
$_SESSION['customerInfo']['email'] = $Email;
?>
// having stored all the info in a session I proceed to send it to the email function. That weird name is because I read you should avoid naming your email process file with predictable names like mail.php, this increases protection against spammers.
<form method ="POST" action = "xljkadf.php">
<INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Confirm Order">
//MAIL PROCESS —
<?php session_start();
# Anti-header-injection - Use before mail()
# By Victor Benincasa <vbenincasa(AT)gmail.com>
foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'.");
$headers = 'From: Your Site <noreply@yoursite.com>' . "\n".
// in the next line what I do is to send a BCC to me as I want the customer to get a copy of the order without knowing my address
$headers .= 'Bcc: Your site <yourmail@xxxx.com>' . "\r\n";
$mailBody = "Order details: \n".
"Name: ".$_SESSION['customerInfo']['name'] . "\n".
"Email: " .$_SESSION['customerInfo']['email'] . "\n";
// Next the mail function. The first arguments is the customer e-mail so he gets a copy of his order.
mail($_SESSION['customerInfo']['Email'], "Order Info ", $mailBody, $headers);
?>
// A redirect to a thank you page once the e-mail is sent.
<script language="JavaScript" type="text/JavaScript">
<!--
window.location.href = "http://www.yoursite.com/thank-you.html";
//-->
</script>
You want to sent an email when the user clicks the SEND button from the preview page. But you do not know how to get the data in PHP that the customer is previewing; because Send does not submit a form.
It this is what your problem is then, here’s what you can do
When customer submits your, in the previewOrder php file you can save the information in a session variable.. eg
Note that session_start() has to be at the beginning of the file.
Now, from the preview page when the user clicks SEND button, you have to direct the page to a file like sendOrder.php which should like something like this
What you are doing here is reading values you have saved in the session in the preview. SESSIONS is one way you can make variables available across pages.
Hope this helps,
PS.I have not tested the code for syntax errors.Please correct them if any 🙂