...

View Full Version : onsubmit two javascript actions



druid1980
06-07-2008, 03:13 PM
Hi,

I have been trying all week to get this working and now have pulled out all my hair.

The problem I am having is that I have created a small shopping cart for paypal.

The cart calculates the shipping cost for various regions and now I also want to add a promotional coupon box to enable the user the input a code published in a newspaper for them to get a discount.

Here is the code:

<code>


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function Dollar (val) { // force to valid dollar amount
var str,pos,rnd=0;
if (val < .995) rnd = 1; // for old Netscape browsers
str = escape (val*1.0 + 0.005001 + rnd); // float, round, escape
pos = str.indexOf (".");
if (pos > 0) str = str.substring (rnd, pos + 3);
return str;
}

function SetShip (obj1) { // record selected shipping option
var j,obj,pos,tok,val,txt;
var ary = new Array ();
obj = obj1.shp; // refer to shipping select
pos = obj.selectedIndex; // get selection
if (pos == 0) { // force a selection
alert ("Please make a shipping selection!");
return false;
}
obj1.handling.value = "0";
obj1.handling_cart.value = "0";
obj1.shipping.value = "0";
obj1.shipping2.value = "0";
val = obj.options[pos].value; // selected value
txt = obj.options[pos].text; // the text value
ary = val.split (" "); // break apart
for (j=0; j<ary.length; j++) { // look at all items
// do 3-character tokens...
if (ary[j].length < 4) continue;
tok = ary[j].substring (0,3); // first 3 chars
val = ary[j].substring (3); // get data
if (tok == "hn=") // value for item handling
obj1.handling.value = val;
if (tok == "hc=") // value for handling cart
obj1.handling_cart.value = val;
if (tok == "s1=") // value for shipping
obj1.shipping.value = val;
if (tok == "s2=") // value for shipping2
obj1.shipping2.value = val;
}
obj1.os0.value = txt; // stuff the text into options field
}
//-->
</script>
<script type="text/javascript">
<!--
var discnt = 0; // no default percent discount

var coupons = new Array ( // place to put coupon codes
"promo", // 1st coupon value - comma seperated
"coup2", // 2nd coupon value - add all you want
"coup3" // 3rd coupon value
);
var coupdc = new Array ( // place to put discounts for coupon vals
13.88,
10,
15
);
var coupval = "(blanket)"; // what user entered as coupon code

function ChkCoup () { // check user coupon entry
var i;
discnt = 0; // assume the worst
for (i=0; i<coupons.length; i++) {
if (coupval == coupons[i]) {
discnt = coupdc[i]; // remember the discount amt
alert ("Valid Promotional Code! 5 Discount Now In Effect");
return;
}
}
alert ("'" + coupval + "' is not a valid code!");
}

function Dollar (val) { // force to valid dollar amount
var str,pos,rnd=0;
if (val < .995) rnd = 1; // for old Netscape browsers
str = escape (val*1.0 + 0.005001 + rnd); // float, round, escape
pos = str.indexOf (".");
if (pos > 0) str = str.substring (rnd, pos + 3);
return str;
}

function ReadForm (obj1) { // apply the discount
var amt,des;
amt = obj1.amount.value*1.0; // base amount
des = obj1.basedes.value; // base description

if (discnt > 0) { // only if discount is active
amt = Dollar (amt - (amt * discnt/100.0));
des = des + ", 5 Discount, Promotion = " + coupval;
}

obj1.amount.value = Dollar (amt);
obj1.item_name.value = des;
}
//-->
</script>

</head>

<body>

<form action="https://www.paypal.com/cgi-bin/webscr" method="post"
onsubmit="return (ReadForm(this) && SetShip (this));">

<input type="hidden" name="cmd" value="_cart" />
<input type="hidden" name="add" value="1" />
<input type="hidden" name="business" value="anthony@face-design.co.uk" />
<input type="hidden" name="item_name" value="Black Pudding & Foie Gras (Andrew Pern) @ 36.00" />
<input type="hidden" name="amount" value="36.00" />
<input type="hidden" name="currency_code" value="GBP" />
<input type="hidden" name="lc" value="GB" />
<input type="hidden" name="no_shipping" value="2">
<input type="hidden" name="return" value=""/>
<input type="hidden" name="cancel_return" value="http://www.southcott.co.uk/blackpudding/blackpudding/buy.htm"/>
<input type="hidden" name="no_note" value="1"/>
<input type="hidden" name="handling" value="" />
<input type="hidden" name="handling_cart" value="" />
<input type="hidden" name="on0" value="Shipping" />
<input type="hidden" name="os0" value="" />
<input type="hidden" name="baseamt" value="36.00" />
<input type="hidden" name="basedes" value="Black Pudding & Foie Gras" />

Enter Coupon code [coup1] &gt;
<input type = "text"
size = "20"
name = "coupcode"; />
&nbsp; &nbsp;
<input type="button" value="Validate"
onclick="coupval = this.form.coupcode.value;
ChkCoup();" />
<br/>

Quantity
<select name="quantity">
<option value="1">1</option>

<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>

<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>

Shipping
<select name="shp">
<option>Select Shipping</option>
<option value="s1=6.99 s2=1.50">UK</option>

<option value="s1=10.99 s2=10.99">Europe*</option>
<option value="s1=14.99 s2=14.99">Rest Of The World*</option>
</select>
<BR><BR>
<input type="image" src="http://www.southcott.co.uk/addtobasket.jpg" name="submit" alt="cart add" />

</form>
</body>
</html>
</code>

As you can see from the Form Action code it calculates the coupon with readform(this) but seems to ignore setship(this).

The example is at http://www.southcott.co.uk/test3.html and the coupon code is promo.

Any help anyone can offer would be greatly appreciated.

Thanks in advance.

Drew

FJbrian
06-08-2008, 05:43 PM
I think, after your calculate coupon function you want to go to the shipping function
You have return; instead



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum