Web Analytics Made Easy -
StatCounter Javascript Form posting error - CodingForum

Announcement

Collapse
No announcement yet.

Javascript Form posting error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript Form posting error

    Hi lads,
    I have a html form, a select list, a textfield and a submit button. Depending on what value is chosen in the select I want the form to be submitted to a different page. My js runs along these lines:

    choice is the value from select list

    if(choice=="1")
    document.form1.action = "http://....";
    document.form1.submit();
    else
    if(choice=="2")
    document.forms.action = "http://..";
    document.forms.submit();

    I am getting a syntax error on the submit() line-does any1 have any ideas what might cause this, the action attribute in the <form> tag is blank(not there)

    Thanks in advance,
    Ken

  • #2
    Well, for one you are using two different form references.
    document.form1 in your choice == 1, and
    document.forms in your choice == 2

    .....Willy

    Comment


    • #3
      sorry that was a mistake on my behalf-both choices are using the correct forms-it appears that its not letting me use the submit() function

      Ken

      Comment


      • #4
        Show us you code. maybe you have not fired the "choise" function from within FORM tag on onsubmit event handler
        KOR
        Offshore programming
        -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

        Comment


        • #5
          The code is attached..the js is retriving the choice from the select-this is grand.its just when i try to submit the same form to different scripts depending on what value is chosen from the select

          cheers lads
          K
          Attached Files

          Comment


          • #6
            Your use of else is throwing a syntax error.
            Either remove the else (if used it should be if else not broke across lines anyway) or, use brackets around your conditional statements.

            eg:
            Code:
            	if(choice=="1")[color=red]{[/color]
            		document.f1.action = "http://....cgi";
            		document.f1.submit();
                                                        [color=red]}[/color]
            	else if(choice=="2")[color=red]{[/color]
            		document.f1.action = "http://....cgi";
            		document.f1.submit();
                                                        [color=red]}[/color]
            .....Willy

            Comment


            • #7
              nope. wrong element reference:

              function jfnChange()
              {

              var choice = document.f1.menu[document.f1.menu.selectedIndex].value ;

              if(choice=="1")
              document.f1.action = "http://....cgi";
              document.f1.submit();
              else if(choice=="2")
              document.f1.action = "http://....cgi";
              document.f1.submit();
              }
              </script>

              document.f1.menu is an array of options

              And:

              onClick="jfnChange()"

              usless to specify javascript
              KOR
              Offshore programming
              -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

              Comment


              • #8
                Originally posted by Kor
                nope. wrong element reference:
                That's funny. Either removing the else or using brackets works for me.
                (tested with google and msn url's)
                Code:
                <Script language="javascript">
                function jfnChange()
                {
                var val;
                var choice = document.f1.menu.value ;
                val=document.f1.name.value;
                if(choice=="1")
                document.f1.action = "http://google.com";
                document.f1.submit();
                
                if(choice=="2")
                document.f1.action = "http://msn.com";
                document.f1.submit();
                }
                </script>
                .
                .
                .
                .
                <BODY BGCOLOR=#FFFFFF MARGINWIDTH=0 MARGINHEIGHT=0 LEFTMARGIN=0 TOPMARGIN=0 ALINK=#336699 VLINK=#000000 >
                <form name="f1" target="basefrm" method="post"><input type="text" size="12" name=name>
                <select name="menu">
                <option value="null">Choose...</option>
                <option value="1">Google</option>
                <option value="2">Mamma</option>
                </select>
                <input type="button" name=go value="Go!" onClick="jfnChange();
                ">
                .....Willy

                Comment

                Working...
                X