Hi,
How would I use Spry so the viewer can only enter data in 1 text field out of a total of 3 text fields in a form and validate so 1 field must be entered. Or CSS perhaps?
Thanks
BTW I'm using CS5
Thanks for the reply Murray.
This is the same form I've been battling with for days and I'm tearing my hair out. I agree about using radio buttons but as I mentioned I can't get the auto total to work with radio buttons, I've got them working with the text fields though.
Here's my code so far; I would be very grateful if you/someone can solve this for me. Cheers
HTML
<!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>Contact us and order online at Hamden Estate vineyard 214 Dry River Road, Martinborough</title>
<meta name="description" content="Hamden Estate vineyard is located on the Dry River Terraces approximately six kilometres south of Martinborough on Dry River Road. The vineyard from its elevated site has spectacular views to the Tararua Ranges" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<link href="css-files/main-styles.css" rel="stylesheet" type="text/css" />
<link href="css-files/menu-styling.css" rel="stylesheet" type="text/css" />
<link href="css-files/order-form-css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function MM_validateForm() { //v4.0
if (document.getElementById){
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
} }
</script>
<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)
return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
// 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
if (pad_total > 0) {
// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
//-->
</script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="outer-wrapper-contact-us">
<div id="lh-col-contact-us">
<div id="hamden-estate-title-lh"></div>
<div id="lh-col-image"></div>
<div id="main-menu">
<div class="arrowgreen">
<ul>
<li><a href="index.html">HOME</a></li>
<li><a href="index.html">THE SIBLING'S STORY</a></li>
<li><a href="about-us.html">ABOUT US</a></li>
<li><a href="vintage-notes.html">VINTAGE NOTES</a></li>
<li><a href="contact-us.html" title="Tools">LOCATION MAP</a></li>
<li><a href="contact-us.html">ORDER ONLINE</a></li>
<li><a href="contact-us.html" class="selected" title="Contact Us">CONTACT US</a></li>
</ul>
</div>
</div>
</div>
<div id="top-banner-about"><div id="masthead-title-contact-us"></div></div>
<div id="lower-image-contact-us"></div>
<div id="main-content-area-contact-us">
<div class="contact-us-text">
</div>
<div id="contact-text-line-one">at 214 Dry River Road, Martinborough</div>
<div id="contact-text-line-two">(06) 306 8001 or 0274 848 439, or send us an </div>
<div id="contact-text-line-three"><a href="mailto:info@hamdenestate.co.nz">info@hamdenestate.co.nz</a></div>
<div id="contact-text-line-four"></div>
<div id="contact-text-line-five"></div>
<div id="main-content-area-contact-us-upper">Use the friendly contact form below to order wine with us. Once we have received your order we will email you with confirmation and your order reference number. Payments should be made directly into our account once you have received this reference number.<br />
</div>
<div id="main-content-area-contact-us-lower"></div>
</div>
<div id="contact-us-rh-image-holder">
<div class="contact-us-rh-image-holder-top">
<a id="contact-us-rh-image-holder-top" href="https://maps.google.co.nz/maps?q=214+Dry+River+Road,+Martinborough&hl= en&client=safari&oe=UTF-8&hnear=214+Dry+River+Rd,+Dyerville+5781,+Well ington&t=h&z=16" target="_new"></a>
</div>
<div id="contact-us-rh-image-holder-bot"></div>
</div>
<div id="contact-us-content-lower">
<div id="contact-us-form-holder">
<div id="orderForm">
<form action="php-files/contact-form-handler.php" id="order" method="post" name="form">
<p> <span class="field-name-label">Name:</span>
<input name="name" type="text" class="name-field" />
<span class="field-name-label">Street: </span>
<input name="address" type="text" class="address-one" id="address" />
</p>
<p><span class="field-name-label">Email:
</span>
<label>
<input name="email" type="text" class="email-field" id="email" onblur="MM_validateForm('email','','RisEmail');return document.MM_returnValue" />
<span class="field-name-label">Suburb: </span></label>
<input name="addresstwo" type="text" class="address-two" id="addresstwo" />
<br />
<br />
<span class="field-name-label">Phone: </span>
<input name="phone" type="text" class="phone-field" id="phone" />
<span class="field-name-label">Town/City:</span>
<input name="addressthree" type="text" class="address-three" id="addressthree" />
<table width="628" border="0">
<tr>
<td width="200" class="wine-name-five">Type of wine</td>
<td width="80"> </td>
<td width="116" align="center" class="dollar-amount">number<br />
of single <br />
bottles</td>
<td width="39" class="dollar-amount">price per bottle</td>
<td width="80" align="center" class="dollar-amount">number<br />
of full <br />
cases</td>
<td width="87" align="center" class="dollar-amount">price <br />
per <br />
case</td>
</tr>
<tr>
<td class="wine-name-five">Chardonnay 2011</td>
<td><input type="checkbox" name="wine1" value="Chardonnay 2011" id="wine1" /></td>
<td align="center"><input name="PROD_Chardonnay2011Bottles_20" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td class="dollar-amount">$20.00</td>
<td><input name="PROD_Chardonnay2011Cases_204" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td align="center" class="dollar-amount">$204.00</td>
</tr>
<tr>
<td class="wine-name-five">Pino Gris 2011</td>
<td><input type="checkbox" name="wine2" value="Pino Gris 2011" id="wine2" /></td>
<td align="center"><input name="PROD_PinoGris2011Bottles_21" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td class="dollar-amount">$21.00</td>
<td><input name="PROD_PinoGris2011Cases_228" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td align="center" class="dollar-amount">$228.00</td>
</tr>
<tr>
<td class="wine-name-five">Sauvigon Blanc 2011</td>
<td><input type="checkbox" name="wine3" value="Sauvigon Blanc 2011" id="wine3" /></td>
<td align="center"><input name="PROD_SauvigonBlanc2011Bottles_20" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td class="dollar-amount">$20.00</td>
<td><input name="PROD_SauvigonBlanc2011Cases_204" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td align="center" class="dollar-amount">$204.00</td>
</tr>
<tr>
<td class="wine-name-five">Riesling 2012</td>
<td><input type="checkbox" name="wine4" value="Riesling 2012" id="wine4" /></td>
<td align="center"><input name="PROD_Riesling2012Bottles_20" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td class="dollar-amount">$20.00</td>
<td><input name="PROD_Riesling2012Cases_204" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td align="center" class="dollar-amount">$204.00</td>
</tr>
<tr>
<td class="wine-name-five">Pino Noir 2009</td>
<td><input type="checkbox" name="wine5" value="Pino Noir 2009" id="wine5" /></td>
<td align="center"><input name="PROD_PinotNoir2009Bottles_25" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td class="dollar-amount">$25.00</td>
<td><input name="PROD_PinotNoir2009Cases_250" type="text" class="selection-box" onchange="CalculateTotal(this.form)" /></td>
<td align="center" class="dollar-amount">$250.00</td>
</tr>
<tr>
<td colspan="6" class="wine-name-five">Mixed cases are also available, at the case prices per bottle.</td>
</tr>
<tr>
<td valign="top" class="wine-name-five">Mixed case per order:</td>
<td colspan="5"><label>
<textarea name="message" id="message" cols="47" rows="5"></textarea>
</label></td>
</tr>
<tr>
<td class="wine-name-five">Freight (per order)<br /> <span class="dollar-amount">please insert 1 in either box</span></td>
<td colspan="4" class="wine-name-five">- North Island
<table width="58" border="0" align="right">
<tr valign="middle">
<td width="48"><span class="dollar-amount">$10.00</span></td>
</tr>
</table></td>
<td><input name="PROD_FreightBottles_10" type="text" class="selection-box" onchange="CalculateTotal(this.form)" size="1" maxlength="1" /> </td>
</tr>
<tr>
<td> </td>
<td colspan="4" class="wine-name-five">- South Island
<table width="58" border="0" align="right">
<tr>
<td width="35" valign="middle"><span class="dollar-amount">$15.00</span></td>
</tr>
</table></td>
<td><input name="PROD_FreightCases_15" type="text" class="selection-box" onchange="CalculateTotal(this.form)" size="1" maxlength="1" /></td>
</tr>
<tr class="table-row">
<td colspan="6"><span class="mixedcase-order">- Collection from vineyard by arrangement</span><span class="free">free</span><input name="PROD_freightfree_0" type="text" class="selection-box" onchange="CalculateTotal(this.form)" size="1" maxlength="1" /></td>
</tr>
</table>
<p align="right">
<p align="right">
<p align="right">
<input name="Submit" type="submit" class="submit-buton" id="Submit" style="width: 120px;" value="Submit order" reset>
</form>
<div id="order-total-wrapper">
<div class="order-total-text" style="text-align: right">
<div align="left" class="order-total-text-1">Total: </div>
</div>
<div class="order-total-text-2" id="order_total" style="text-align: right">$0.00</div>
<div id="total-payable">Total amount payable<br /><br />
<div id="conformation-text">When you click "submit" a confirmation email will also be sent to your inbox</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="footer-links">
<a href="site-map.html" class="rh-vert-border">site map</a>
<a href="contact-us.html" class="rh-vert-border">order online</a>
<a href="contact-us.html" class="rh-vert-border">contact us</a>
<a href="http://www.loudmouse.co.nz/" target="_new">© loudmouse design ltd 2012</a></div>
<script src="js/wineorder.js"></script>
<script>
jQuery(function ($) {
$('#order').validate();
});
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "none");
</script>
<script>
document.forms[0].onsubmit = fillOne;
function fillOne() {
var elems = this.elements,
req1 = [];
for (var i = 0; i < elems.length; i++) {
if (elems[i].hasAttribute('data-req1')) {
req1.push(elems[i].name);
if (elems[i].value.search(/\S+/) != -1) {
return true;
}
}
}
alert('At least one of the following fields must have a value:\n' + req1.join(', '));
return false;
}
</script>
</body>
</html>
PHP Form handler file
<?php
$errors = '';
$myemail = 'bigcheese@loudmouse.co.nz'; //<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['address']))
{
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$addresstwo = $_POST['addresstwo'];
$addressthree = $_POST['addressthree'];
$wine1= $_POST['wine1'];
$wine2= $_POST['wine2'];
$wine3= $_POST['wine3'];
$wine4= $_POST['wine4'];
$wine5= $_POST['wine5'];
$wine6= $_POST['wine6'];
$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'];
$message = $_POST['message'];
$freight_bottles = $_POST['PROD_FreightBottles_10'];
$freight_cases = $_POST['PROD_FreightCases_15'];
$freight_free = $_POST['PROD_freightfree_0'];
$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) + ($freight_bottles * 10 + $freight_cases * 15 + $freight_free * 000);
$reply = $_POST['email'];
$replysubject = "Auto-Reply: From Hamden Estate Website";
$replyfrom = "From: bigcheese@loudmouse.co.nz\r\n";
$replymessage = "Dear ".$_POST ['name']."\r\n\r\n";
$replymessage .= "Thank you for placing an order with Hamden Estate vineyard. We will contact you shortly with a reference number and payment details...\r\n\r\n";
$replymessage .= "";
$replymessage .= "YOUR ORDER DETAILS:\r\n\r\n";
$replymessage .= "Wine Selection: $wine1\r\n";
$replymessage .= "Bottles: $chardonnay_2011_bottles\r\n";
$replymessage .= "Cases: $chardonnay_2011_cases\r\n\r\n";
$replymessage .= "Wine Selection: $wine2\r\n";
$replymessage .= "Bottles: $pino_gris_2011_bottles\r\n";
$replymessage .= "Cases: $pino_gris_2011_cases\r\n\r\n";
$replymessage .= "Wine Selection: $wine3\r\n";
$replymessage .= "Bottles: $sauvigon_blanc_2011_bottles\r\n";
$replymessage .= "Cases: $sauvigon_blanc_2011_cases\r\n\r\n";
$replymessage .= "Wine Selection: $wine4\r\n";
$replymessage .= "Bottles: $riesling_2012_bottles\r\n";
$replymessage .= "Cases: $riesling_2012_cases\r\n\r\n";
$replymessage .= "Wine Selection: $wine5\r\n";
$replymessage .= "Bottles: $pinot_noir_2009_bottles\r\n";
$replymessage .= "Cases: $pinot_noir_2009_cases\r\n\r\n";
$replymessage .= "FREIGHT: $wine6\r\n";
$replymessage .= "North Island: $freight_bottles\r\n";
$replymessage .= "South Island: $freight_cases\r\n\r\n";
$replymessage .= "Client pick up: $freight_free\r\n\r\n";
$replymessage .= "Total $ $totalcost\r\n\r\n";
$replymessage .= "From the folks at Hamden Estate\r\n";
$replymessage .= "bigcheese@loudmouse.co.nz\r\n";
$replymessage .= "http://www.hamdenestate.co.nz\r\n\r\n";
$replymessage .= "This e-mail is automated, so please DO NOT reply.\r\n";
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}) $/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received an online order via your website. Here are the details: ".
" \n Name: $name
\n Email: $email_address
\n Phone: $phone
\n Address: $address
\n Address: $addresstwo
\n Address: $addressthree
\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 message \n $message
\n Freight: $wine6
\n North Island: \n $freight_bottles
\n South Island: \n $freight_cases
\n Client pick up: \n $freight_free
\n Total $ : $totalcost";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
mail($reply,$replysubject,$replymessage,$replyfrom);
//redirect to the 'thank you' page $reply = $_POST['email'];
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
</html>
Hi Murray,
The text fields listed below;
"PROD_FreightBottles_10"
"PROD_FreightBottles_15"
"PROD_FreightBottles_0"
I need these 3 fields in radio buttons so the viewer can only choose one and that Freight amount i.e. $10.00, $15.00 or $0.00 is added to the total.
At the moment the auto calculate works with the text fields but when I replace them with radio buttons the auto calculate doesn't work.
Thanks Murray
The problem is that the JavaScript function you're using works only with text fields. It's not designed to work with radio buttons or other types of form input. There's a copyright notice with your CalculateTotal() script. If it's a script that you have paid for, you should ask the creator of the script to make it compatible with radio buttons. If it's a free script, it can be adapted to work with radio buttons, but it will take a little time to work out.
Thanks for the reply David, yes I did pay for CalculateTotal() script but I've since found the script on another site, the chap that I paid to create the script is not the person on the Copyright! And this guy wants more money to change to the script to be compatible with radio buttons, there is very little money in this project so I can't afford to pay him more I'm afraid.
gofer wrote:
I did pay for CalculateTotal() script but I've since found the script on another site, the chap that I paid to create the script is not the person on the Copyright! And this guy wants more money to change to the script to be compatible with radio buttons
It's sad that there are such disreputable people who will steal other people's copyright and charge. I've been playing around with the script, and it wasn't all that difficult to adapt. Try out the following:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add Values</title>
<script>
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" && form_field.type != 'radio') {
// 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
}
} else if (form_name.substring(0,4) == "PROD" && form_field.type == 'radio' && form_field.checked == true) {
order_total += parseInt(form_field.value);
}
}
// 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)
return pad_with_zeros(result3, decimals)
}
function pad_with_zeros(rounded_value, decimal_places) {
// 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
if (pad_total > 0) {
// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
<input type="text" name="PROD_first_20" id="PROD_first_20" onChange="CalculateTotal(this.form)">
</p>
<p>
<label>
<input type="radio" name="PROD_freight" value="10" id="PROD_freight_0" onChange="CalculateTotal(this.form)">
North</label>
<br>
<label>
<input type="radio" name="PROD_freight" value="15" id="PROD_freight_1" onChange="CalculateTotal(this.form)">
South</label>
<br>
<label>
<input type="radio" name="PROD_freight" value="0" id="PROD_freight_2" onChange="CalculateTotal(this.form)">
Nowhere</label>
</p>
<div id="order_total" style="text-align: right">$0.00</div>
</form>
</body>
</html>
What I have done is to adapt the CalculateTotal() function to work with radio buttons.
Each radio button has the same name (the actual name is unimportant). The value attribute contains the value you want to add to the total.
Thank you David, that is fantastic working perfectly.
If I could just ask one more question if I may, in the PHP file I've experimenting with different code but can't seem to be able to get the Freight in the total when returned in an email.
I appreciate your time and trouble David.
Cheers
$radio = $_POST['PROD_freight_10'];
$radio = $_POST['PROD_freight_15'];
$radio = $_POST['PROD_freight_0'];
$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) + ($PROD_freight_10 * 10 + $PROD_freight * 15 + $PROD_freight * 00);
Hi gofer, instead of radio buttons (if you can't get those to work) you could use a 'select' list option. That should work, I've tested it. The 'select list' doesn't mess with the original javascript form calculations script (although David seems to have sorted this out for you now) and they return the correct totalprice via php.
So you would insert a 'Select delivery option' on the form:
select name="PROD_freight_1" onchange="CalculateTotal(this.form)">
<option value="0" selected="selected">Collect Order $0</option>
<option value="10">North Island $10</option>
<option value="15" >South Island $15 </option>
</select>
Then amend the php freight variable to retreive the correct price:
$freight = $_POST['PROD_freight_1'];
Radio buttons share the same name. Look again at the sample I gave you:
<p>
<label>
<input type="radio" name="PROD_freight" value="10" id="PROD_freight_0" onChange="CalculateTotal(this.form)">
North</label>
<br>
<label>
<input type="radio" name="PROD_freight" value="15" id="PROD_freight_1" onChange="CalculateTotal(this.form)">
South</label>
<br>
<label>
<input type="radio" name="PROD_freight" value="0" id="PROD_freight_2" onChange="CalculateTotal(this.form)">
Nowhere</label>
</p>
They are all called PROD_freight, so the value your PHP code should be looking for is $_POST['PROD_freight']. That will contain the number stored in the value attribute.
If you rename the radio buttons PROD_freight_15, PROD_freight_10, and PROD_freight_0, they become independent buttons, all of which can be selected. The whole point of a radio button group is that only one can be selected. But for that to happen, each button in the group must have the same name.
David_Powers wrote:
gofer wrote:
I did pay for CalculateTotal() script but I've since found the script on another site, the chap that I paid to create the script is not the person on the Copyright! And this guy wants more money to change to the script to be compatible with radio buttons
It's sad that there are such disreputable people who will steal other people's copyright and charge. I've been playing around with the script, and it wasn't all that difficult to adapt. Try out the following:
This was a freely available script which I found on the net and passed onto Gofer. As long as the Copyright notice remains at the top of the script I think the writer is ok about it being used. Not sure what Gofer is alluding to about payment. As far as I know it went straight from me to Gofer.
osgood_ wrote:
Hi gofer, instead of radio buttons (if you can't get those to work) you could use a 'select' list option. That should work, I've tested it. The 'select list' doesn't mess with the original javascript form calculations script
Although a select menu would offer the same "single-choice" option as a set of radio buttons, it presents the same problem for the JavaScript function. The way the function has been designed is that it extracts the price from the input element's name, and uses it to calculate the total cost. That's why gofer had PROD_freight_10, PROD_freight_15, and PROD_freight_0. The function grabs the numbers after the second underscore.
But radio button groups and select menus can have only one name, so you have to get the value from elsewhere. What I did was to adapt the script so that it handles radio buttons separately and gets the price from the value attribute of the selected button. The script could be adapted to do the same with a select menu. It would be slightly more complicated, but still easily done.
osgood_ wrote:
This was a freely available script which I found on the net and passed onto Gofer. As long as the Copyright notice remains at the top of the script I think the writer is ok about it being used. Not sure what Gofer is alluding to about payment. As far as I know it went straight from me to Gofer.
I don't know anything about the provenance of the script other that what I've read here. Obviously, there's a misunderstanding somewhere.
I have now changed the script, but I think the copyright notice should stay in because most of the work was done by the original developer. I would suggest rewording the copyright notice like this:
/* Adapted from a script that 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.*/
David_Powers wrote:
osgood_ wrote:
Hi gofer, instead of radio buttons (if you can't get those to work) you could use a 'select' list option. That should work, I've tested it. The 'select list' doesn't mess with the original javascript form calculations script
Although a select menu would offer the same "single-choice" option as a set of radio buttons, it presents the same problem for the JavaScript function.
Hi David,
It doesn't if you use the value 1 for PROD and then give the select options 10, 15 or 0 respectively. The script then seems to x it by that total based on 1 and add the correct value to the overall form total, job done! I could not do that with the radio buttons because as you say unless the javascript is amended (which I could'nt do) it messes up the calculations.
North America
Europe, Middle East and Africa
Asia Pacific