Dreamweaver

Currently Being Moderated

Extracting the total in dollars from a form

Aug 19, 2012 12:24 AM

Tags:

Hi,

I have a order form with fields for 5 products, each product can be ordered in single qty or a case with a sub total field plus a grand total and I'm trying to figure out how to extract the total & grand total amounts (in dollars) into the php form handler file so the amounts are sent back to the visitor and the site owner.

Replies
• Currently Being Moderated
Aug 19, 2012 7:25 AM   in reply to gofer

The \$_POST variable will contain these values already - all you have to do is use them.  The total amount will be in \$_POST['total'].  The vine name will be in \$_POST['wine'], and the subtotal amount would be the sum of all of the individual totals for all of the vines.  Is that what you mean?

|
Mark as:
• Currently Being Moderated
Aug 19, 2012 8:40 AM   in reply to MurraySummers

Murray *ACP* wrote:

Is that what you mean?

Murray....I think the OP needs a javascript solution to change the TOTAL form field before the form is submitted, so the user can see the total sum of the bottles or cases of wine they have bought.

I'm not great at javascript but I've been doing some research. Below will work but you need to create 5 functions (one for each of the wines). I've done the first 3 below: It follows a simple formula so it should'nt be too difficult to implement the other two.

<script type="text/javascript"><!--

function chardonnay2011() {

document.form.chardonnay2011total.value = (document.form.chardonnay2011bottles.value -0) * 20.00 + (document.form.chardonnay2011cases.value -0) * 204.00;

}

function pinogris2011() {

document.form.pinogris2011total.value = (document.form.pinogris2011bottles.value -0) * 21.00 + (document.form.pinogris2011cases.value -0) * 228.00;

}

function sauvignonblanc2011() {

document.form.sauvignonblanc2011total.value = (document.form.sauvignonblanc2011bottles.value -0) * 20.00 + (document.form.sauvignonblanc2011cases.value -0) * 204.00;

}

//--></script>

Then this is how to apply it to the input form fields:

<span class="wine-name-one">Chardonnay 2011</span></label>

<input name="chardonnay2011bottles" onChange="chardonnay2011()" class="selection-box">

<span class="price">\$20.00</span> <input name="chardonnay2011cases" onChange="chardonnay2011()" class="selection-box"> <span class="price">\$204.00</span> \$<input name="chardonnay2011total" value="00.00" class="selection-box"></p>

<span class="wine-name-two">Pino Gris 2011</span></label>

<input name="pinogris2011bottles" onChange="pinogris2011()" class="selection-box"> <span class="price">\$21.00</span> <input name="pinogris2011cases" onChange="pinogris2011()" class="selection-box"> <span class="price">\$228.00</span> \$<input name="pinogris2011total" value="00.00" class="selection-box"></p>

<span class="wine-name-three">Sauvignon Blanc 2011</span></label>

<input name="sauvignonblanc2011bottles" onChange="sauvignonblanc2011()" class="selection-box"> <span class="price">\$20.00</span> <input name="sauvignonblanc2011cases" onChange="sauvignonblanc2011()" class="selection-box"> <span class="price">\$204.00</span> \$<input name="sauvignonblanc2011total" value="00.00" class="selection-box"></p>

Also you need to include name="form" in the form action field:

<form action="php-files/contact-form-handler.php" id="order" method="post" name="form">

This works but probaby needs refining.

|
Mark as:
• Currently Being Moderated
Aug 19, 2012 8:47 AM   in reply to osgood_

But, the total field *is* already changing to reflect the total price, so I was a bit confused by what OP was asking....

The thing is, those selections are started with a radio button, so you can only select one vine for each submission.  That's part of my confusion.

|
Mark as:
• Currently Being Moderated
Aug 20, 2012 12:00 AM   in reply to MurraySummers

Murray *ACP* wrote:

But, the total field *is* already changing to reflect the total price, so I was a bit confused by what OP was asking....

Not actually on the form itself its not...the only way to get the total is for the user to insert it in the field themselves. I think, but I'm not sure, the OP wants the total form field to update automatically when the number of bottles or cases are selected before the form is submited.

But as you say I think there may be an issue here because the vines are grouped within one radio button.

What if the user wants to purchase two or three different vines.......they can't without going through the procedure again????

|
Mark as:
• Currently Being Moderated
Aug 20, 2012 4:34 AM   in reply to osgood_

What if the user wants to purchase two or three different vines.......they can't without going through the procedure again????

That was my confusion as well.  Your suggested code looks OK to me.

|
Mark as:
• Currently Being Moderated
Aug 20, 2012 6:12 AM   in reply to MurraySummers

I searched around this morning and found the below javascript from http://www.mcfedries.com/javascript/ordertotals.asp which I adapted below for the wines selections. No styling but I think it has possibilities and more than one wine selection can be chosen.

Only thing I can't get the script to do is write the 'order_total' to a form input field which could then be collected via php and sent to the email address. At moment the script writes the total to <div>

<div id="order_total" style="text-align: right">\$0.00</div>

<!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">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Calculating Order Form Totals in  JavaScript</title>

<style>

input {

width: 60px;

}

#orderForm {

width: 600px;

margin: 0 auto;

}

</style>

<script language="JavaScript" type="text/javascript">

<!--

/* This script is Copyright (c) Paul McFedries and

Logophilia Limited (http://www.mcfedries.com/).

Permission is granted to use this script as long as

this Copyright notice remains in place.*/

function CalculateTotal(frm) {

var order_total = 0

// Run through all the form fields

for (var i=0; i < frm.elements.length; ++i) {

// Get the current field

form_field = frm.elements[i]

// Get the field's name

form_name = form_field.name

// Is it a "product" field?

if (form_name.substring(0,4) == "PROD") {

// If so, extract the price from the name

item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

// Get the quantity

item_quantity = parseInt(form_field.value)

// Update the order total

if (item_quantity >= 0) {

order_total += item_quantity * item_price

}

}

}

// Display the total rounded to two decimal places

document.getElementById("order_total").firstChild.data = "\$" + round_decimals(order_total, 2)

}

function round_decimals(original_number, decimals) {

var result1 = original_number * Math.pow(10, decimals)

var result2 = Math.round(result1)

var result3 = result2 / Math.pow(10, decimals)

}

// Convert the number to a string

var value_string = rounded_value.toString()

// Locate the decimal point

var decimal_location = value_string.indexOf(".")

// Is there a decimal point?

if (decimal_location == -1) {

// If no, then all decimal places will be padded with 0s

decimal_part_length = 0

// If decimal_places is greater than zero, tack on a decimal point

value_string += decimal_places > 0 ? "." : ""

}

else {

// If yes, then only the extra decimal places will be padded with 0s

decimal_part_length = value_string.length - decimal_location - 1

}

// Calculate the number of decimal places that need to be padded with 0s

var pad_total = decimal_places - decimal_part_length

// Pad the string with 0s

for (var counter = 1; counter <= pad_total; counter++)

value_string += "0"

}

return value_string

}

//-->

</script>

<body>

<div id="orderForm">

<form>

<tr>

<td width="200">Type of vine</td>

<td width="80">Number<br>

of single bottles</td>

<td>Price<br>per<br>bottle</td>

<td width="80">Number<br>of full<br> cases</td>

<td>Price<br>per<br>case</td>

</tr>

<tr>

<td><input type="checkbox" name="chardonnay_2011" id="chardonnay_2011" />Chardonnay 2011</td>

<td><Input type="text" name="PROD_Chardonnay2011Bottles_20.00" onChange="CalculateTotal(this.form)"></td>

<td>\$20.00</td>

<td><Input type="text" name="PROD_Chardonnay2011Cases_204.00" onChange="CalculateTotal(this.form)"></td>

<td>\$204.00</td>

</tr>

<tr>

<td><input type="checkbox" name="pinoGris_2011" id="pinoGris_2011" />Pino Gris 2011</td>

<td><Input type="text" name="PROD_PinoGris2011Bottles_21.00" onChange="CalculateTotal(this.form)"></td>

<td>\$21.00</td>

<td><Input type="text" name="PROD_PinoGris2011Cases_228.00" onChange="CalculateTotal(this.form)"></td>

<td>\$228.00</td>

</tr>

<tr>

<td><input type="checkbox" name="sauvigonBlanc_2011" id="sauvigonBlanc_2011" />Sauvigon Blanc 2011</td>

<td><Input type="text" name="PROD_SauvigonBlanc2011Bottles_20.00" onChange="CalculateTotal(this.form)"></td>

<td>\$20.00</td>

<td><Input type="text" name="PROD_SauvigonBlanc2011Cases_204.00" onChange="CalculateTotal(this.form)"></td>

<td>\$204.00</td>

</tr>

<tr>

<td><input type="checkbox" name="riesling_2012" id="riesling_2012" />Riesling 2012</td>

<td><Input type="text" name="PROD_Riesling2012Bottles_20.00" onChange="CalculateTotal(this.form)"></td>

<td>\$20.00</td>

<td><Input type="text" name="PROD_Riesling2012Cases_204.00" onChange="CalculateTotal(this.form)"></td>

<td>\$204.00</td>

</tr>

<tr>

<td><input type="checkbox" name="pinoNoir_2009" id="pinoNoir_2009" />Pinot Noir 2009</td>

<td><Input type="text" name="PROD_PinotNoir2009Bottles_25.00" onChange="CalculateTotal(this.form)"></td>

<td>\$25.00</td>

<td><Input type="text" name="PROD_PinotNoir2009Cases_250.00" onChange="CalculateTotal(this.form)"></td>

<td>\$250.00</td>

</tr>

</table>

<input style="width: 120px;"  type="reset" reset value="clear form">

</form>

<div style="text-align: right">Total:</div>

<div id="order_total" style="text-align: right">\$0.00</div>

</div>

</body>

</html>

|
Mark as:
• Currently Being Moderated
Aug 21, 2012 2:01 AM   in reply to gofer

Lets go through this step by step.

1) First ensure that your wine input form fields look as below: (I had to remove the .00 from the name attribute as php didn't process the information when that was present).

<input type="checkbox" name="wine1" value="Chardonnay 2011" id="wine1" />

<input type="text" name="PROD_Chardonnay2011Bottles_20" onchange="CalculateTotal(this.form)" />

<input type="text" name="PROD_Chardonnay2011Cases_204" onchange="CalculateTotal(this.form)" />

<input type="checkbox" name="wine2" value="Pino Gris 2011" id="wine2" />

<input type="text" name="PROD_PinoGris2011Bottles_21" onchange="CalculateTotal(this.form)" />

<input type="text" name="PROD_PinoGris2011Cases_228" onchange="CalculateTotal(this.form)" />

<input type="checkbox" name="wine3" value="Sauvigon Blanc 2011" id="wine3" />

<input type="text" name="PROD_SauvigonBlanc2011Bottles_20" onchange="CalculateTotal(this.form)" />

<input type="text" name="PROD_SauvigonBlanc2011Cases_204" onchange="CalculateTotal(this.form)" />

<input type="checkbox" name="wine4" value="Riesling 2012" id="wine4" />

<input type="text" name="PROD_Riesling2012Bottles_20" onchange="CalculateTotal(this.form)" />

<input type="text" name="PROD_Riesling2012Cases_204" onchange="CalculateTotal(this.form)" />

<input type="checkbox" name="wine5" value="Pino Noir 2009" id="wine5" />

<input type="text" name="PROD_PinotNoir2009Bottles_25" onchange="CalculateTotal(this.form)" />

<input type="text" name="PROD_PinotNoir2009Cases_250" onchange="CalculateTotal(this.form)" />

2) Next get the information from the input fields using php: (The below goes in the form-hander.php file after \$message = \$_POST['message'];) and before \$mixedcase= \$_POST['mixedcase'];  You can get rid of the old lines of php between those two variables.

\$wine1= \$_POST['wine1'];

\$wine2= \$_POST['wine2'];

\$wine3= \$_POST['wine3'];

\$wine4= \$_POST['wine4'];

\$wine5= \$_POST['wine5'];

\$chardonnay_2011_bottles = \$_POST['PROD_Chardonnay2011Bottles_20'];

\$chardonnay_2011_cases = \$_POST['PROD_Chardonnay2011Cases_204'];

\$pino_gris_2011_bottles = \$_POST['PROD_PinoGris2011Bottles_21'];

\$pino_gris_2011_cases = \$_POST['PROD_PinoGris2011Cases_228'];

\$sauvigon_blanc_2011_bottles = \$_POST['PROD_SauvigonBlanc2011Bottles_20'];

\$sauvigon_blanc_2011_cases = \$_POST['PROD_SauvigonBlanc2011Cases_204'];

\$riesling_2012_bottles = \$_POST['PROD_Riesling2012Bottles_20'];

\$riesling_2012_cases = \$_POST['PROD_Riesling2012Cases_204'];

\$pinot_noir_2009_bottles = \$_POST['PROD_PinotNoir2009Bottles_25'];

\$pinot_noir_2009_cases = \$_POST['PROD_PinotNoir2009Cases_250'];

3) Now get the total: The below can go in the form-hander.php file after the variables we created above.

\$totalcost = (\$chardonnay_2011_bottles * 20 + \$chardonnay_2011_cases * 204) + (\$pino_gris_2011_bottles * 21 + \$pino_gris_2011_cases * 228) + (\$sauvigon_blanc_2011_bottles * 20 + \$sauvigon_blanc_2011_cases * 204) + (\$riesling_2012_bottles * 20 + \$riesling_2012_cases * 204) + (\$pinot_noir_2009_bottles * 25 + \$pinot_noir_2009_cases * 250);

4) Amend the \$email_body variable (see below) so it processes the information created above (you'll see if follows a pattern asking for the variables which reference each wine and number of bottles/cases - \$wine1, \$chardonnary_2011_bottles, \$chardonnary_2011_cases etc:

\$email_body = "You have received a new message. ".

" Here are the details:\n Name: \$name

\n Phone: \$phone

\n Wine Selection: \$wine1

\n Bottles: \$chardonnay_2011_bottles

\n Cases: \$chardonnay_2011_cases

\n Wine Selection: \$wine2

\n Bottles: \$pino_gris_2011_bottles

\n Cases: \$pino_gris_2011_cases

\n Wine Selection: \$wine3

\n Bottles: \$sauvigon_blanc_2011_bottles

\n Cases: \$sauvigon_blanc_2011_cases

\n Wine Selection: \$wine4

\n Bottles: \$riesling_2012_bottles

\n Cases: \$riesling_2012_cases

\n Wine Selection: \$wine5

\n Bottles: \$pinot_noir_2009_bottles

\n Cases: \$pinot_noir_2009_cases

\n Total \$ : \$totalcost

\n Message \n \$message";

That should return the information you require.

|
Mark as:
• Currently Being Moderated
Aug 22, 2012 12:19 AM   in reply to gofer

gofer wrote:

Thank you very much osgood, that is fantastic it works beautifully, I really appreciate all your help you have been very generous with your time.

All the best & regards

gofer, the more I think about it the more this situation requires some kind of a shopping cart, maybe paypal?

How are you intending to take payment for the wine? What about shipping costs? This all points to a cart being needed.

|
Mark as:
• Currently Being Moderated
Aug 22, 2012 12:45 AM   in reply to gofer

gofer wrote:

Thanks for the reply, at this stage the client doesn't want to dip their toe into e-commerce (maybe later) for now they are happy with doing a manual transaction, yes, it's  a bit archaic but that's what they want. As for the freight, I was going to replicate what you have done for me with 2 checkboxes, viewers click either box, 1 for \$10.00 and the other for \$15.00, very straightforward.

Cheers

Oh ok....see how the client gets on.

|
Mark as:
• Currently Being Moderated
Aug 23, 2012 12:09 AM   in reply to gofer

Remember the rely message? Add the php variables to that (as below)

\$replymessage .= "Thank you for taking the time to contact This Constructions via our web site..\r\n\r\n";

\$replymessage .= "Your request has been received and a member of our staff will contact you soon.\r\n\r\n\r\n\r\n";

|
Mark as:
• Currently Being Moderated
Aug 23, 2012 5:54 AM   in reply to gofer

Hi Gofer....the \$reply variables below go after:

\$totalcost= (\$chardonnay_2011_bottles * 20 + \$chardonnay_2011_cases * 204) + (\$pino_gris_2011_bottles * 21 + \$pino_gris_2011_cases * 228)

+ (\$sauvigon_blanc_2011_bottles * 20 + \$sauvigon_blanc_2011_cases * 204) + (\$riesling_2012_bottles * 20 + \$riesling_2012_cases * 204)

+ (\$pinot_noir_2009_bottles * 25 + \$pinot_noir_2009_cases * 250);

Insert the below AFTER the above in the form-handler php script.

/* Creates strings for the automated reply email once the user submits the form */

\$replymessage .= "Thank you for taking the time to contact This Constructions via our web site..\r\n\r\n";

\$replymessage .= "Your request has been received and a member of our staff will contact you soon.\r\n\r\n\r\n\r\n";

---------------------------------------------------------------------- -------------------------------

Then the reply mail goes AFTER the first mail as below:

---------------------------------------------------------------------- ------------------------------

Obviously insert the correct email addresses in the reply variables (I don't post email addresses in the forum as it attracts spam)

|
Mark as:
• Currently Being Moderated
Aug 24, 2012 12:51 AM   in reply to gofer

gofer wrote:

Thank you osgood,

The reply to the visitor is working beautifully, the only thing I can't get to work is the freight, sorry!

There is 2 text boxes, one for North Island Freight = \$10.00

and the other is South Island = \$15.00

Just not sure what php code this should be.

I'm lost on this one at the moment.

I can't think of a way to add the freight to the form total. What is happening is you have to type 1 into the text field next to the freight prices to get the right total at the bottom of the form and no one is going to do that.

If you change the freight text fields to checkboxes or radio buttons then you have to give them a value="10" or value="15". This then completley messes up the form calculation.

At the moment you may just have to give a total cost for the wine on the form and obviously state the freight is additional to the cost of the wine.

Getting the wine plus freight total to the email and reply address using php is pretty simple:

Change the freight fields to radio buttons so the customer can select the freight price based on where they want the wine sent to, either North or South: (changing the input name to 'freight' avoids the form javascript messing up the form calculation total because they arent prefixed with PROD)

Then add the below variable to the form-hander php script:

\$freight = \$_POST['freight'];

AFTER:

\$mixedcase= \$_POST['mixedcase'];

Then add the below to the end of the \$totalcost variable

+ \$freight;

|
Mark as:
• Currently Being Moderated
Aug 24, 2012 3:41 AM   in reply to osgood_

Hi osgood_,

your form field hints I follow with great interest. I'll try to understand the method of "<?php ... ?>" better and better.

Your "cookbook instructions" encourage me and so I'll ask you: would you please show me how this "contact-form-handler.php" file looks like in its entirety, and where I had to point out here (from above):

"Also you need to include name="form" in the form action field:
<form action="php-files/contact-form-handler.php" id="order" method="post" name="form">"

Hans-Günter

﻿

|
Mark as:
• Currently Being Moderated
Aug 24, 2012 3:42 AM   in reply to gofer

gofer wrote:

Hi osgood,   many thanks for this code, I've combined your instructions and found this on the net: http://stackoverflow.com/questions/4551840/how-can-add-the-value-to-ra dio-buttons

I think that should get the end result.

Yes, the code I supplied above will work for sending the complete total (wine + freight) to the return email address and the reply email address BUT it won't add the freight to the form total on the webpage. That somehow needs to be done by javascript before the form is submitted......that's where the issue lies.

gofer wrote:

Thank you again osgood, I really appreciate your help, would have been stuck without your help.

Youre welcome.

|
Mark as:
Actions

More Like This

• Retrieving data ...