Forum Moderators: open

Message Too Old, No Replies

PHP/Javascript Dynamic Input Boxes?

         

rjbearcan

5:50 pm on Oct 18, 2006 (gmt 0)

10+ Year Member



I don't use JavaScript enough to know exactly how to do this but I am usign PHP to create a table where products are shown and quantities can be entered and the form is sent off. What I would like now is a subtotal box where after they enter the quantity, an input box shows the new total for that particular product.

Can someone point me in the right direction?

inveni0

7:33 pm on Oct 20, 2006 (gmt 0)

10+ Year Member



I would assume that the onBlur (or something similar) would help here. I'm sure someone more familiar with JS could elaborate, but you could start your research there.

brucec

2:09 am on Oct 22, 2006 (gmt 0)

10+ Year Member



Thanks, inveni0, that person who is familiar with JS is me! LOL

rjbearcan, you would have to get good at the DOM (Document Object Model) commands. They would work very well here. Once you start using the DOM in Javascript, you will be hooked. It has become the standard nowadays in Javascript.

To start, in your form input fields, make sure you use ID attributes for all your quantity inputs like this:

<form>
<input id="q1" name="quantity1">
<input id="q1" name="quantity2">
<input readonly id="total" name="total">
<input type="button" onClick="getTotal();">
</form>

Note: Make sure your total input has a readonly attribute since you don't want your web visitors to change that since your javascript will do it.

Your javascript will do all the math for you via the DOM like this:
<script language="Javascript">
function getTotal() {
q1 = document.getElementById("q1").value;
q2 = document.getElementById("q2").value;
document.getElementById("total").value=q1 + q2;
}
</script>

That's all there is to it. I assumed you have 2 quantity inputs in your form. If you have more that two inputs in your form, then adjust accordingly.

If you really want to get fancy, have your PHP do it dynamically with an array.

I hope this helps,
Bruce