barrycorrigan Posted February 3, 2011 Share Posted February 3, 2011 Hi Everyone, I have created some code that lets a user fill out an order form. e.g Name, Email etc etc....But they also have the option to type in a reference code from a catalog, type in what price it is and how much of a quantity they want. This will generate a total. The user can do this 3 times and it will generate a grand total. Then this will post to the clients email and they will follow the order up. I no it's not the best way but it's what they client wants. The next stage of the project is too generate discount from the grand total field. For example: If the Grand Total is between £15-£129 this is = 10% then if the Grand Total is between £130-£249 this is = 15% off if the Grand Total is greater than £250 this is = £25% off Here is my code with PHP that emails the form. I hope someone can help me with this any help would be great. This is a working example if you just change the email address at the top and i've attached the include file corefuncs.php. <?php include('includes/corefuncs.php'); if (function_exists('nukeMagicQuotes')) { nukeMagicQuotes(); } // process the email if (array_key_exists('send', $_POST)) { $to = 'example@example.com'; // use your own email address $subject = 'Enquiry from the order form'; // list expected fields $expected = array('name', 'email', 'comments', 'refA', 'qtyA', 'prcA', 'totalA', 'refB', 'qtyB', 'prcB', 'totalB', 'refC', 'qtyC', 'prcC', 'totalC', 'GrandTotal'); // set required fields $required = array('name', 'email', 'comments'); // create empty array for any missing fields $missing = array(); // assume that there is nothing suspect $suspect = false; // create a pattern to locate suspect phrases $pattern = '/Content-Type:|Bcc:|Cc:/i'; // function to check for suspect phrases function isSuspect($val, $pattern, &$suspect) { // if the variable is an array, loop through each element // and pass it recursively back to the same function if (is_array($val)) { foreach ($val as $item) { isSuspect($item, $pattern, $suspect); } } else { // if one of the suspect phrases is found, set Boolean to true if (preg_match($pattern, $val)) { $suspect = true; } } } // check the $_POST array and any sub-arrays for suspect content isSuspect($_POST, $pattern, $suspect); if ($suspect) { $mailSent = false; unset($missing); } else { // process the $_POST variables foreach ($_POST as $key => $value) { // assign to temporary variable and strip whitespace if not an array $temp = is_array($value) ? $value : trim($value); // if empty and required, add to $missing array if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } // otherwise, assign to a variable of the same name as $key elseif (in_array($key, $expected)) { ${$key} = $temp; } } } // validate the email address if (!empty($email)) { // regex to ensure no illegal characters in email address $checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/'; // reject the email address if it doesn't match if (!preg_match($checkEmail, $email)) { array_push($missing, 'email'); } } // go ahead only if not suspect and all required fields OK if (!$suspect && empty($missing)) { // set default values for variables that might not exist $subscribe = isset($subscribe) ? $subscribe : 'Nothing selected'; $interests = isset($interests) ? $interests : array('None selected'); $characteristics = isset($characteristics) ? $characteristics : array('None selected'); // build the message $message = "Name: $name\n\n"; $message .= "Email: $email\n\n"; $message .= "Comments: $comments\n\n"; $message .= "Trophie One reference number: $refA\n\n"; $message .= "Quantity for Trophie One: $qtyA\n\n"; $message .= "Price for Trophie One: $prcA\n\n"; $message .= "Total for Trophie One: $totalA\n\n"; $message .= "Trophie Two reference number: $refB\n\n"; $message .= "Quantity for Trophie Two: $qtyB\n\n"; $message .= "Price for Trophie Two: $prcB\n\n"; $message .= "Total for Trophie Two: $totalB\n\n"; $message .= "Trophie Three reference number: $refC\n\n"; $message .= "Quantity for Trophie Three: $qtyC\n\n"; $message .= "Price for Trophie Three: $prcC\n\n"; $message .= "Total for Trophie Three: $totalC\n\n"; $message .= "Grand Total: $GrandTotal\n\n"; //$message .= "Subscribe: $subscribe\n\n"; //$message .= 'Interests: '.implode(', ', $interests)."\n\n"; //$message .= 'Characteristics associated with Japan: '.implode(', ', $characteristics); // limit line length to 70 characters $message = wordwrap($message, 70); // create additional headers $additionalHeaders = "From: $name<$email>"; if (!empty($email)) { $additionalHeaders .= "\r\nReply-To: $email"; } // send it $mailSent = mail($to, $subject, $message, $additionalHeaders, $email); if ($mailSent) { // $missing is no longer needed if the email is sent, so unset it unset($missing); } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Pennine Trophies - Order Form</title> <script type="text/javascript"> //This function calculates a money figure on the oder form an assistant function (No need to touch it's not the main calculate function) function dm(amount) { string = "" + amount; dec = string.length - string.indexOf('.'); if (string.indexOf('.') == -1) return string + '.00'; if (dec == 1) return string + '00'; if (dec == 2) return string + '0'; if (dec > 3) return string.substring(0,string.length-dec+3); return string; }//End money function (again please do not touch!!!!) function calculate() { QtyA = 0; QtyB = 0; QtyC = 0; TotA = 0; TotB = 0; TotC = 0; PrcA = 0; PrcB = 0; PrcC = 0; //Below the code for the price if (document.ofrm.prcA.value > "") { PrcA = document.ofrm.prcA.value }; document.ofrm.prcA.value = eval(PrcA); if (document.ofrm.prcB.value > "") { PrcB = document.ofrm.prcB.value }; document.ofrm.prcB.value = eval(PrcB); if (document.ofrm.prcC.value > "") { PrcC = document.ofrm.prcC.value }; document.ofrm.prcC.value = eval(PrcC); //Below the code for the quanity if (document.ofrm.qtyA.value > "") { QtyA = document.ofrm.qtyA.value }; document.ofrm.qtyA.value = eval(QtyA); if (document.ofrm.qtyB.value > "") { QtyB = document.ofrm.qtyB.value }; document.ofrm.qtyB.value = eval(QtyB); if (document.ofrm.qtyC.value > "") { QtyC = document.ofrm.qtyC.value }; document.ofrm.qtyC.value = eval(QtyC); //Get the totals for the calculator (May need to be altered) TotA = QtyA * PrcA; document.ofrm.totalA.value = dm(eval(TotA)); TotB = QtyB * PrcB; document.ofrm.totalB.value = dm(eval(TotB)); TotC = QtyC * PrcC; document.ofrm.totalC.value = dm(eval(TotC)); Totamt = eval(TotA) + eval(TotB) + eval(TotC) ; document.ofrm.GrandTotal.value = dm(eval(Totamt)); if (document.ofrm.GrandTotal.value > 55) } </script> </head> <body> <h1>Pennine Trophies</h1> <h2>Order form</h2> <p>Please make your selections from the following choices. If you have any problems feel free to contact us via the contact page.</p> <?php if ($_POST && isset($missing)) { ?> <p class="warning" style="color:#F00;">Please complete the field(s) marked (*)</p> <?php } elseif ($_POST && !$mailSent) { ?> <p class="warning" style="color:#F00;"> * Sorry, there was a problem sending your message. Please try later.</p> <?php } elseif ($_POST && $mailSent) { ?> <p class="success" style="color:#EA7F1E;">thankyou for your email we will contact you shortly </p> <?php } ?> <form method="post" action="" name="ofrm" id="ofrm"> <fieldset> <label for="name">Name:<?php if (isset($missing) && in_array('name', $missing)) { ?><span class="warning">*</span><?php } ?></label><br /> <input type="text" id="name" name="name" size="30"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['name']).'"';} ?> /> </fieldset> <fieldset> <label for="email">Email:<?php if (isset($missing) && in_array('email', $missing)) { ?><span class="warning">*</span><?php } ?></label><br /> <input type="text" id="email" name="email" size="30"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['email']).'"';} ?> /> </fieldset> <fieldset> <label for="comments">Comments:<?php if (isset($missing) && in_array('comments', $missing)) { ?><span class="warning">*</span><?php } ?></label><br /> <textarea name="comments" id="comments" cols="60" rows="8"><?php if (isset($missing)) { echo htmlentities($_POST['comments']);} ?></textarea> </fieldset> <table border="0" cellpadding="0" width="700" id="table"> <tr align="left"> <th>Wording</th> <th>Reference</th> <th>Quantity</th> <th>Rate</th> <th>Total</th> </tr> <tr> <td>1.</td> <td><input type="text" id="refA" name="refA" size="20" tabindex="1"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['refA']).'"';} ?> /></td> <td><input type="text" id="qtyA" name="qtyA" size="5" tabindex="5" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['qtyA']).'"';} ?> /></td> <td><input type="text" id="prcA" name="prcA" size="5" tabindex="5" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['prcA']).'"';} ?> /></td> <td><input type="text" id="totalA" name="totalA" size="12" tabindex="99" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['totalA']).'"';} ?> /></td> </tr> <tr> <td>2.</td> <td><input type="text" id="refB" name="refB" size="20" tabindex="1"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['refB']).'"';} ?> /></td> <td><input type="text" id="qtyB" name="qtyB" size="5" tabindex="5" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['qtyB']).'"';} ?> /></td> <td><input type="text" id="prcB" name="prcB" size="5" tabindex="5" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['prcB']).'"';} ?> /></td> <td><input type="text" id="totalB" name="totalB" size="12" tabindex="99" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['totalB']).'"';} ?> /></td> </tr> <tr> <td>3.</td> <td><input type="text" id="refC" name="refC" size="20" tabindex="1"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['refC']).'"';} ?> /></td> <td><input type="text" id="qtyC" name="qtyC" size="5" tabindex="5" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['qtyC']).'"';} ?> /></td> <td><input type="text" id="prcC" name="prcC" size="5" tabindex="5" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['prcC']).'"';} ?> /></td> <td><input type="text" id="totalC" name="totalC" size="12" tabindex="99" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['totalC']).'"';} ?> /></td> </tr> <tr> <td></td> <td></td> <td></td> <td><br />Totals</td> <td><br /><input type="text" id="GrandTotal" name="GrandTotal" size="12" tabindex="99" onchange="calculate()"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['GrandTotal']).'"';} ?> /></td> </tr> <tr> <td> <input name="send" id="send" type="submit" value="Send Order" /> <input type="reset" value="Reset" name="resetButton" /> </td> </tr> </table> </form> </body> </html> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 3, 2011 Share Posted February 3, 2011 C'mon, if you want help don't make us do all the work. I'm not going to read throuogh all that code to try and determine where you are wanting to calcualte the discount. Based upon your statements above it would seem the discount is generated after the user submits - but I would think you would want to display it to the user. The code you provided does not work. There is an error int he JS that is preventing it from compiling and I'm not going to try and figure it out. But, calculating the discount is pretty strait forward once you have the total: Here is the basic structure you can use, integrate it as you see fit. You code has so many flaws I'm not going to try and integrate it. BYt he way - remove all the eval() statements in your JS code, the way you are using it has no purpose and eval() should (almost) never be used. var discountPct = 0; if(Totamt>=15) { discountPct = .10; } if(Totamt>=130) { discountPct = .15; } if(Totamt>=250) { discountPct = .25; } var discountAmt = parseFloat(Math.round(Totamt*discountPct*100)/100).toFixed(2); alert('Discount percent is ' + discountPct); alert('Discount amount is ' + discountAmt); alert('New total is ' + (Totamt-discountAmt)); Quote Link to comment Share on other sites More sharing options...
barrycorrigan Posted February 3, 2011 Author Share Posted February 3, 2011 Hi mjdamato Totally understand. In work there is no point in waiting on a reply or even a reply. So I just took it on myself and got it to work myself which im quite chuffed about. I ended up doing this: if ( Totamt >= 55 && Totamt <= 129 ) { Discount = Totamt * 10 / 100; document.ofrm.discount.value = dm(eval(Discount)); } else if ( Totamt >= 130 && Totamt <= 249 ) { Discount = Totamt * 15 / 100; document.ofrm.discount.value = dm(eval(Discount)); } else if ( Totamt >= 250 ) { Discount = Totamt * 25 / 100; document.ofrm.discount.value = dm(eval(Discount)); } else { Discount = 0; document.ofrm.discount.value = dm(eval(Discount)); } And since I was on a role I even figured out how to calculate a delivery charge by this if ( Final <= 99 ) { Delivery = 7.95; document.ofrm.delivery.value = dm(eval(Delivery)); } else if ( Final >= 100 && Final <= 349 ) { Delivery = 11.95; document.ofrm.delivery.value = dm(eval(Delivery)); } else if ( Final >= 350 ) { Delivery = 0; document.ofrm.delivery.value = dm(eval(Delivery)); } else { Delivery = 7.95; document.ofrm.delivery.value = dm(eval(Delivery)); } Thanks for the help and from looking at your code i've done it in a similar sort of way. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 3, 2011 Share Posted February 3, 2011 You have duplicative code there that you can clean up. For example, in each of the IF statements you are calculating the discount AND populating the discount field. You should only calculate the discount in the IF statement, then have a single line after all those IF statements to populate the value. (Again, what is the purpose of eval() where you have used it?) Also, there is a bug in your logic. You are not checking for the decimal valules between the min/max values. In the code above a total of 249.50 would generate a $0 discount. This would do the exact same as the first block of code in your last post with a lot less work (and corrected logic) var Discount = 0; //Set default discount if ( Totamt >= 55 && Totamt < 130 ) { Discount = Totamt * 10 / 100; } else if ( Totamt >= 130 && Totamt < 250 ) { Discount = Totamt * 15 / 100; } else if ( Totamt >= 250 ) { Discount = Totamt * 25 / 100; } document.ofrm.discount.value = dm(Discount); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.