• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

change the price (which is a form 'read only' element) when a coupon code is entered

Hi, I have a checkout form, that you can see at the end of the shopping process starting from: http://www.paintyourlife.com/shop.asp

At the checkout page, I would like to allow the customers to enter a coupon code which will update the price of their order.

I already do it using a checkbox of 'express service' to which I add onClick="calculatePrice(this)"

but with the coupon code it's different. Is there any event type I can use?  
If not, then I need to add an "update" button near the coupon code input field, but that means a form inside another form (which as far as I know is not allowed)

Any suggestions?

*Any remarks regarding the whole shopping process will be appreciated.

Thanks a lot,

  • 2
  • 2
1 Solution
I would use either onChange or onBlur.

onChange will fire your function as soon as you type something into the box... So, what you'd want to do for this method is have your function check for a valid code and if it's valid update your price.


onBlur will fire your function when the user tabs or click out of the box (opposite of onFocus basically), so after they enter a code and then tab or click elsewhere it will fire the function and update the price.

kustielAuthor Commented:

Thanks for the prompt answer.
Now, when I do the onBlur, I get to a JavaScript functiion, in which I need to check in my SQL database, whether such a coupon code exist, and what discount is associated with it.
My question is: how do I access the database from within the JavaScript code. Until now, I only did it from my ASP/VBSCRIPT code.

Thanks, Assaf
ohhh ok.  Well if there are many different codes (as in thousands) you will have to use some type of AJAX solution to asyncronously check your db and return the results.  That is a whole other question and I would suggest asking it seperatly in the Javascript section.

If however you only have a few hundred valid codes, you can load them into a javascript array when the page loads up and then you'd just look up the code in your array in javascript to get the discount.  This will be a better solution and much easier to set up compared to the Ajax option if you don't have too many codes.

You will want to use an associative array so you can relate the discout to each code:
Here's a good explaination of associative arrays in javascript:

quick example:
var codes = new Array();
codes["abc123"] = ".50";  // 50% off
codes["aabb"] = ".10"; // 10% off
codes["cc00"] = ".25"; // 25% off

var userCode = document.form.codeField.value;
if ( codes[userCode] )
    // it is a valid code so you can pass the discount to your function.
  // not a valid code

So when you reference the array with a code the result is the discount:

would print:  .10
kustielAuthor Commented:
Hi jasonsbytes,

I'm trying to implement your solution and I;m still a bit confused....

There are 2 things I need to do:

1.) Store the values in the array - to do this, I need to call a JavaScript function in the OnLoad of the body tag, right? Then, from inside this function I call the SQL database to read the values? I have never accessed the SQL DB from inside a JavaScript function. I always did it in the ASP / VSCRIPT code. Is it the same?

2.) Laster, when someone will enter a coupon code, I will call some OnBlur function. Will this function be able to use the variable I used in the former function? Are these global variables?

I'm sorry if my questions sound strange. I just come from a world of c, c++ coding and am pretty new to this web developing.

Thanks, Assaf

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now