...

View Full Version : Form Script - Help Please (Easy)



ThatBloke
09-21-2011, 06:29 PM
Hi All

Javascript novice here so just need a bit of help. I'm sure it's something easy

I have a form, attributes below:


<form id="frmOrder" name="input" method="post" onsubmit="return OnSubmitForm();">

I need the deposit input value to match the total input value if the weeks10 checkbox is ticked. The reason for this is my client requires full payment if this box is ticked. This must be done upon form submission.

The reason for this is the email.php and payment.php submission pages collect the deposit value using $_POST and use that as the payment total

Attributes for inputs below:

Weeks10 checkbox:


<input type="checkbox" name="weeks10" id="weeks10" class="weeks10" />


Deposit input:


<input type="text" name="deposit" id="txtTotal3" readonly="readonly" size="2" value="0.00"></input>

Totals input:


<input type="text" readonly="readonly" id="txtTotal" value="0.00" size="2" name="total"/>


I'm using the following script:



<script type="text/javascript">
function OnSubmitForm()
{
if(document.input.weeks10[0].checked == true)
{
document.input.deposit.value = document.input.total.value;
}
return true;
}
function OnSubmitForm()
{
if(document.input.operation[0].checked == true)
{
document.input.action ="email.php";
}
else
if(document.input.operation[1].checked == true)
{
document.input.action ="payment.php";
}
return true;
}
</script>


The problem is, the second function works (email.php or payment.php action depending on radio selection). But the first one to adjust the total doesnt

Now I know my script is wrong so would somebody be kind enough to lend me a hand?

Thanks! :D

*Edit - forgot to mention the example is live here if you need to see the full code. It's a fairly complicated form (for me atleast anyway) http://lake-form.carpbookings.co.uk

blaze4218
09-21-2011, 07:11 PM
You cant have the same function twice, you need to combine those

DanInMa
09-21-2011, 07:26 PM
<script type="text/javascript">

function OnSubmitForm()
{
if (document.input.weeks10[0].checked == true)
{
document.input.deposit.value = document.input.total.value;
}
else if (document.input.operation[0].checked == true)
{
document.input.action ="email.php";
}
else
if(document.input.operation[1].checked == true)
{
document.input.action ="payment.php";
}
return true;
}
</script>

DanInMa
09-21-2011, 07:33 PM
I'd just like to point out that it wont work if they have JS disabled, it will always submit to the default action of the form, so you may want to put a default action property on the form

ThatBloke
09-21-2011, 08:10 PM
Ah ok, thanks

I'll give it a bash

ThatBloke
09-21-2011, 08:18 PM
Strange, form doesn't submit now, any ideas ? :confused:

blaze4218
09-21-2011, 08:34 PM
it might be the way they were combined, when you tried to make two versions of the same function it implied to me that this would be the best way to join them:


function OnSubmitForm(){
if(document.input.weeks10[0].checked == true){
document.input.deposit.value = document.input.total.value;
}

if(document.input.operation[0].checked == true){
document.input.action ="email.php";
}
else if(document.input.operation[1].checked == true){
document.input.action ="payment.php";
}

return true;
}

this completely separates the two ideas instead of chaining them all. I don't see why they wouldn't both work, but at least this version is a lot closer to what you originally had, just without the error...

ThatBloke
09-21-2011, 08:52 PM
neither work, weird .....

blaze4218
09-21-2011, 08:54 PM
sorry i made a small error, check my post again

ThatBloke
09-21-2011, 09:01 PM
No cigar, the form just refreshes

blaze4218
09-21-2011, 09:02 PM
I'd just like to point out that it wont work if they have JS disabled, it will always submit to the default action of the form, so you may want to put a default action property on the form

did you make sure to do that?

blaze4218
09-21-2011, 09:18 PM
I copied your page and tried it with my code and without, the only difference was the error that it couldn't get the value of checked for the weeks10[0] but when i changed that line to if(document.input.weeks10.checked == true){ that error disappeared. I couldn't tell if that solved the problem because it still just refreshed the page, but I think that might have been your form validation script. What I can tell you is that when you had 2 OnSubmitForm() functions the second function ran just fine (according to your post) but the first function never ran. So theirs no telling if that line was ever correct to begin with... try to either adjust that one line like i showed you above, and try this:


function OnSubmitForm(){
if(document.input.operation[0].checked == true){
document.input.action ="email.php";
}
else if(document.input.operation[1].checked == true){
document.input.action ="payment.php";
}

return true;
}

and see if either works.

ThatBloke
09-21-2011, 09:29 PM
Thanks, i'll try it now

Weird though, if I submit the form in debug mode, it errors on the very first line, however if I choose to continue, both scripts run perfectly

ThatBloke
09-21-2011, 09:36 PM
Brilliant you nailed it mate

Turns out it didn't like the [0] as you suggested. Didn't try without it as it worked for the other if statements

Thanks for your time!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum