PDA

View Full Version : Set form value based on user input



Katt
11-29-2010, 10:49 PM
I'm trying to create a custom order page on a hosted shopping cart, therefore I'm working around some limitations. I can use javascript, but I'm not good enough to figure this out.

The code to place each item in the cart is done with a simple html form, but I need to be able to change the price based on the quantity entered by the end user. Is this possible?

ie. if user enters qty 1, price is 5.00, qty 2-5, price is 4.50, qty 6 or more, price is 4.00.

This will need to be repeated many times (30? 50?) on the same page so the code would need to be as short as possible.

Here's a sample of existing code



<form name="form_1" action="addtocart.aspx" method="post">
<input type="hidden" name="ItemNbr" value="123">
<input type="hidden" name="product" value="SAMPLE">
Qty: <input type="text" name="qty" value="">
<input type="submit" name="submit" value="Add To Cart">
<input type="hidden" name="price" value="">
</form>


Thanks for any and all suggestions!

jscheuer1
11-30-2010, 03:33 AM
This cannot be done reliably nor securely with javascript.

Extra Information:

Your users may have javascript disabled, worse - they could perhaps use javascript to make the price whatever they want it to be.

It should be done in aspx on the addtocart.aspx page or elsewhere. The price per item for each item should be set there manually or retrieved there from a database based upon the item number. The hidden field 'price' on your form should play no role in the matter. The set price from somewhere the user cannot control could be multiplied times the 'qty' value, which should probably be a select drop down list, not a text input. Or if it remains a text input, its value should be validated in aspx as a number and if it passes that, be rounded to the nearest whole number before multiplying times the price. If it doesn't pass as a number, the user should be thrown back and told to enter a valid number, or it should be set to 1. That's why a select is better, you may list only positive whole numbers greater than 0 as options. Using a select element is only a problem if you want to allow more than 10 possible 'qty' numbers.

Katt
11-30-2010, 03:02 PM
Thanks for the reply. I was looking at javascript because I don't have access to the addtocart.aspx page, or anything server side. But using a select might work - thanks for the idea!