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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Submit method not working

    I have this form which has to be submitted after triggering some JavaScript. So at the very end of the script I put document.myform.submit();, but I get the error that the method is not supported by that object. So I googled but only found that this was the correct way.

    I won't post the entire code (unless it's really neccessary), but here are the submit part of the script and the opening form tag:

    Code:
    document.quiz.submit();
    
    
    <FORM action="http://www.eos.net/cgis/forms/forms.acgi" method="post" name="quiz">
    If anyone can shed any light on my problem, I'd much appreciate it.

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm confused.. maybe we need to see the whole script?

    When is this script being called? onSubmit? because there's no onSubmit event handler in your form tag. Could it be in your submit button tag? or is it onclick of the submit button?

    From the code you're showing, or at least the ordering, you're submitting the form before you've declared it. So chances are you're saying null.submit():
    Code:
    document.quiz.submit();
     document.quiz == null
     null.submit();
    That's what I think, but I can't help without seeing the whole code.

    Sadiq.

  • #3
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Well to post everything would be a lot to go through and not necessary. I'll explain what exactly it does.

    There's a form of questions with radio button multiple choice answers. After all is entered, the surfer presses a button:

    Code:
    <input type="button" name="submit" value="Submit" onClick="score()">
    The score() function processes the answers (validates and calculates the score), then at the very end I placed:

    Code:
    document.quiz.submit();


    I'm not sure what you mean by declaring a form.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok does this script submit the form no matter what?

    If that's the case:
    Code:
    <FORM action="http://www.eos.net/cgis/forms/forms.acgi" method="post" name="quiz" onsubmit="return score();">
    
    ...
    
    <input type="button" name="submit" value="Submit">
    In score(), replace:
    Code:
    document.quiz.submit();
    with:
    Code:
    return true;
    This way, onsubmit, score() will run. Once score() is complete, it will submit the form.

    Now if you don't want to submit the form for some reasons (validation..), return false in the appropriate area in score().

    That should do it.

    Sadiq

  • #5
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by NanakiXIII
    The score() function processes the answers (validates and calculates the score), then at the very end I placed:

    Code:
    document.quiz.submit();
    Why?

    The reason Sadiq asked to see your code is because it doesn't make sense. Normaly, you would use the onsubmit="return score()" and your score function would validate for missed questions, ect. and return false if the validation fails and continues to submit if it doesn't.

    However, you could try: document.forms['quiz'].submit()

  • #6
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok does this script submit the form no matter what?
    No, there is validation, if something has not been entered, the script will give an alert and break off.

    The way you suggested worked (when I changed that button to a submit). Thank you. Could you tell me why my original approach did not?



    Why?
    It seemed like the logical thing. I wanted the form to be submitted when the script had finished it's business.

    That sounds like it makes sense. It might not be the regular way it's done, and obviously not the right way, but I'm not much of a JavaScripter, I was just being inventive, having never used this before. Thank you for your suggestion, but sad69's way made things happen.



    I now have a new question. I want to have the script change the value of a hidden input in the form. The logical solution to me would seem:

    Code:
    document.myform.myinput.value = newvalue;
    
    In my script I used:
    
    document.forms[0].score.value = total;
    
    where total is a variable previously defined.
    Which is also what I've found on google, but it once again tells me the method or property is not supported. I really can't think of any other way or find any. What am I doing wrong?
    Last edited by NanakiXIII; 06-30-2004 at 01:46 AM.

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Quote Originally Posted by NanakiXIII
    Could you tell me why my original approach did not?
    This is how I see it.
    You have a submit button named "submit".

    <input type="button" name="submit" value="Submit">

    So when you say:

    document.quiz.submit();

    it refers to the submit button (instead of the submit method of the form) and it fails because it is not a method.
    The solution is to change the name of the submit button so something else.

    <input type="button" name="btnSubmit" value="Submit">

    Try that and see how it goes. But I agree with sadiq that using onsubmit event handler is the best solution. I'm just demonstrating the cause of the error.

  • #8
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks, glenngv. Does anyone have an answer for my second problem?

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I know that the code is long but can you attach the page or is it online?

  • #10
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I won't post the entire code, because a lot of code gets repeated.

    Code:
    function score() //function to calculate scores
     {
    
      if(confirm("Your answers will now be processed and stored. Do you want to continue?")) { alert("Initiating Process"); }
       else { alert("Aborting"); return false; }
    
      //start check question one, there is one for every question (18 of them),
      //simply following eachother, so you can imagine the rest
    
      var answer1 = document.quiz.q1
      for (i = 0 ; i < answer1.length ; i++)
    
      if (answer1[i].checked == true) //checks which radiobutton is checked
       {
        var q1 = answer1[i].value //stores value of radiobutton to var
        chkd1=1 //changes value of var chkd1 to note a radiobutton is checked
       }
    
      if(chkd1==0) //checks if a radiobutton is checked
       {
        alert("Please select an answer from questions 1") //alerts viewer if not so
        chkd1=0
        return false;
       }
    
    //calculate total score and alert viewer
    
      var total = (q1*1) + (q2*1) + (q3*1) + (q4*1) + (q5*1) + (q6*1) + (q7*1) + (q8*1) + (q9*1) + (q10*1) + (q11*1) + (q12*1) + (q13*1) + (q14*1) + (q15*1) + (q16*1) + (q17*1) + (q18*1);
      document.forms[0].score.value = total;
      alert ("You scored "+total+"% !");
      return true;
     }
    I know this script is crappy, but it's an old one.

    Then the form looks like this:

    Code:
    <FORM action="http://www.eos.net/cgis/forms/forms.acgi" method="post" name="quiz" id="quiz" onsubmit="return score();">
    <CENTER><P>
    <INPUT TYPE="hidden" NAME="*mailto" VALUE="in_umbre_mortis@hotmail.com">
    <INPUT TYPE="hidden" NAME="*mailfrom" VALUE="nanakixiii@ja.nl">
    <INPUT TYPE="hidden" NAME="*mailsubject" VALUE="results" size=50 maxlength=50><BR>
    <INPUT TYPE="hidden" NAME="*smtphost" VALUE="80.57.233.217">
    <INPUT TYPE="hidden" NAME="*header" VALUE="<title>Form Reply</title>
                <body><h2>Part 4</h2>"></P></CENTER>
    
    <input type="hidden" name="score" id="score" value="0">
    
    
    1. Question one?<br>
    <input type="radio" name="q1" value=0>Answer one<br>
    <input type="radio" name="q1" value=2>Answer two<br>
    <input type="radio" name="q1" value=4>Answer three<br>
    <input type="radio" name="q1" value=6>Answer four<br>
    <br>
    
    2. Question two, etc.
    
    <input type="submit" name="submit" value="Submit">
    </form>

    So that's it. The entire code has 18 pieces of JavaScript to process a question and eighteen sets of radio buttons. If you can imagine those (they look exactly the same except they have answer2, 3 and q2, 3, etc.), you've got the entire thing.

  • #11
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by glenngv
    This is how I see it.
    You have a submit button named "submit".

    <input type="button" name="submit" value="Submit">

    So when you say:

    document.quiz.submit();

    it refers to the submit button (instead of the submit method of the form) and it fails because it is not a method.
    The solution is to change the name of the submit button so something else.

    <input type="button" name="btnSubmit" value="Submit">

    Try that and see how it goes. But I agree with sadiq that using onsubmit event handler is the best solution. I'm just demonstrating the cause of the error.
    Although I agree with everything that was said here. Particularly the advice to use the onsubmit handler which I also previously pointed out, albiet a few minutes behind Sad. However, my suggestion of using: document.forms['quiz'].submit() should work without the need to rename the submit button.

    Which brings us to question two:

    I have found that using multiple instances of a name for a variable, form field and function have caused unexpected problems and/or conflicts. Try renaming either your function score() or your hidden form field score.....

    Lastly, all of that repeated code which validates each group of radio buttons could be written in 12 lines or less. If you attach your codes in zip format, I'm sure someone here could show you how.....

    .....Willy

  • #12
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yes, I've used sad69's suggestion and it works perfectly.

    I'm afraid changing the name to something unused did not help.

    I have a sample of the use of this script online now. Please excuse the sheer stupidity of the nature of the questions (not appropriate for all ages), I only created the script for it.

    http://panthers1.netfirms.com/sluttest4.htm

    Don't try submitting it either, I'm still working on the cgi part. Any suggestions to improve the script are of course appreciated.

  • #13
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The problem (one of them anyway) is too the fact that both the function and your hidden form field are both named score!!

    That is why, when submitted you are getting and object error.
    I just took your codes and changed the hidden form field name and the script began to work but I do not have time at this moment to look any deeper.

    Please take my advice and change the form field name.

    .....Willy

  • #14
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Interesting quiz indeed.

    Here, it could probably be made shorter yet, but I threw this together between the other things I am suppose to be doing.

    Replace this script with your current script and make the changes highlighted in red to the function call and change the hidden field name from score to total.

    Code:
    <script type="text/javascript">
      function score(form){
       var total = 0, radioGroups = [];
         for(var i=0; i<form.elements.length; i++){
          var element = form.elements[i];
           if(element.type.toLowerCase() == 'radio' && 
           (!radioGroups[element.name] && radioGroups[element.name] !== null)){
             var isChecked = (element.checked)? true : null;
             radioGroups[element.name] = isChecked;
           }
    
           else if(element.type.toLowerCase() == 'radio' &&
           (radioGroups[element.name] || radioGroups[element.name] == null)){
             if(element.checked){
                radioGroups[element.name] = true;
             }
          }
    
          if(element.type.toLowerCase() == 'radio' && element.checked){
             total = total+element.value*1;
          }
        }
    
        for(var i in radioGroups){
          if(!radioGroups[i]){
             alert('Please answer question '+i.replace('q',''));
             return false;
          }
        }
    
       var message = 'Your answers will now be processed and stored.\n';
           message+= 'Do you want to continue?';
           if(confirm(message)){ alert("Initiating Process") };
           else{ alert("Aborting"); return false };
    
           form.total.value = total;
           alert("You are "+total+"% Slutty!");
             if(total == 100){
                alert("You got full score! 100%!");
                alert("You are the ultimate slut!");
             }  return true;
      }
    </script>
    </head>
    <body>
    <form action="/cgi/formmail" method="post" name="quiz" onsubmit="return score(this);">
    <input type="hidden" name="total" value="0">
    .....Willy
    Last edited by Willy Duitt; 06-30-2004 at 06:39 PM.

  • #15
    Regular Coder
    Join Date
    Dec 2003
    Posts
    100
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I did change the name (on an offline version), but it had no effect. But I might have overlooked something.

    Thanks for your script, I will try it out tomorrow (it's getting late right now), but I have a few questions. I'm not this knowledgeable about JavaScript so some parts of the script you wrote don't look very familiar.

    radioGroups = [];

    What does the [] mean?


    for(var i in radioGroups)

    I've never seen this construction. I've actually never seen anything besides (initial;test;increment). What does this do?


    And !variable is the same as variable=false, right?



    I'll have to take some time to understand this script. Thanks for writing it for me.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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