Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Form Script - Help Please (Easy)

    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:

    Code:
    <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:

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

    Deposit input:

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

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

    I'm using the following script:


    Code:
    <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!

    *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
    Last edited by ThatBloke; 09-21-2011 at 06:37 PM. Reason: extra info

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    You cant have the same function twice, you need to combine those

  • #3
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    <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>

  • #4
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    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

  • #5
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ah ok, thanks

    I'll give it a bash

  • #6
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Strange, form doesn't submit now, any ideas ?

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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:
    Code:
    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...
    Last edited by blaze4218; 09-21-2011 at 08:52 PM. Reason: changed code, small error

  • #8
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    neither work, weird .....

  • #9
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    sorry i made a small error, check my post again

  • #10
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No cigar, the form just refreshes

  • #11
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    Quote Originally Posted by DanInMa View Post
    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?

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    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:
    Code:
    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.

  • #13
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #14
    New Coder
    Join Date
    Jul 2011
    Posts
    27
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •