javascript_beg
29 Jul 2009, 08:25 AM
I'm a beginner, and I'm currently playing around with forms. I'm trying something which in theory should be really simple, but in practice is turning out to be quite complex. It probably is actually really simple, but my inexperienced programming brain is making it more complicated than it is!
What I'm trying to do is have a form add up two values and create a new hidden value from the total before submitting.
I have two options.
- A yes or no question, where "Yes" adds £5 to the value
- Another choice of options each having their own value
The way I've thought of doing this (rightly or wrongly) is on submit, instead of submitting the form, run a javascript that will add up the values, then automatically submit the form. So far this is proving to be more difficult than first thought!
I have a simple PHP script (separate URL) which the form submits to which just displays the value. This works fine.
The form is called "pl_pay" and the script I have at the moment in the head of the document is this:
function payform() {
var total = first+amount;
document.forms.pl_pay.write('<input type="hidden" name="totalamm" value="'+total+'">');
document.forms.pl_pay.submit();
}
Then instead of the submit button, I have this to call the function:
<a href="javascript:payform();"><img src="images/submit_button.gif" border="0" />
However, when clicking the button, the browser instead of submitting the form just thinks about it for a bit, and then just sits on the page.
So where have I gone wrong?
It's obvious I'll bet... :goof:
What I'm trying to do is have a form add up two values and create a new hidden value from the total before submitting.
I have two options.
- A yes or no question, where "Yes" adds £5 to the value
- Another choice of options each having their own value
The way I've thought of doing this (rightly or wrongly) is on submit, instead of submitting the form, run a javascript that will add up the values, then automatically submit the form. So far this is proving to be more difficult than first thought!
I have a simple PHP script (separate URL) which the form submits to which just displays the value. This works fine.
The form is called "pl_pay" and the script I have at the moment in the head of the document is this:
function payform() {
var total = first+amount;
document.forms.pl_pay.write('<input type="hidden" name="totalamm" value="'+total+'">');
document.forms.pl_pay.submit();
}
Then instead of the submit button, I have this to call the function:
<a href="javascript:payform();"><img src="images/submit_button.gif" border="0" />
However, when clicking the button, the browser instead of submitting the form just thinks about it for a bit, and then just sits on the page.
So where have I gone wrong?
It's obvious I'll bet... :goof: