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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Huge Java Noob! Probably a stupid question, but argg....

    Good morning all!

    I'm working with an html based quiz/test on logic, and I had a timer working for it but I'm having issues having the formail part of the script to message me the timer results. At the moment it messages me the start and finish time in full GMT Time format which, I can also use but I find it frustrating that I spent so much time trying to get a stopwatch to work in this script that now it won't message me the results of it.

    Here are the two parts of the script I'm having issues with:

    Code:
    var c1, c2, c3, ID = null;
    startTimer();
    
    function startTimer() {
        if (ID != null) {
            stopTimer();
        }
        c1 = 0;
        c2 = 0;
        c3 = 0;
        ID = window.setInterval(run, 100);
    }
    
    function stopTimer() {
        window.clearInterval(ID);
        ID = null;
    }
    
    function run() {
        c1++;
        if (c1 == 60) {
            c1 = 0;
            c2++;
            if (c2 == 60) {
                c2 = 0;
            }
        }
    
        var o1 = (c1 <= 9 ? "0" : "")  + c1;
        var o2 = (c2 <= 9 ? "0" : "")  + c2;
        var o3 = (c3 <= 9 ? "0" : "")  + c3;
        document.forms[0].elements[0].value = o3 + ":" + o2 + ":" + o1;
    }
    Code:
    //CODE FOR HANDLING SENDING OF RESULTS
    
    var UserName = '';
    var StartTime = (new Date()).toLocaleString();
    
    var ResultForm = '<html><body><form name="Results" action="http://fp1.formmail.com/cgi-bin/fm192" method="post" enctype="x-www-form-encoded">';
    ResultForm += '<input type="hidden" name="_pid" value="******">';
    ResultForm += '<input type="hidden" name="_fid" value="******">';
    ResultForm += '<input type="hidden" name="recipient" value=""></input>';
    ResultForm += '<input type="hidden" name="subject" value="A"></input>';
    ResultForm += '<input type="hidden" name="Exercise" value="A"></input>';
    ResultForm += '<input type="hidden" name="realname" value=""></input>';
    ResultForm += '<input type="hidden" name="Score" value=""></input>';
    ResultForm += '<input type="hidden" name="Start_Time" value=""></input>';
    ResultForm += '<input type="hidden" name="End_Time" value=""></input>';
    ResultForm += '<input type="hidden" name="title" value="Thanks!"></input>';
    ResultForm += '<input type="hidden" name="bgcolor" value="#C0C0C0"></input>';
    ResultForm += '<input type="hidden" name="text_color" value="#000000"></input>';
    ResultForm += '<input type="hidden" name="sort" value="order:realname,Exercise,Score,Start_Time,End_Time"></input>';
    ResultForm += '</form></body></html>';
    
    function GetUserName(){
    	UserName = prompt('UVA ID','');
    	UserName += '';
    	if ((UserName.substring(0,4) == 'null')||(UserName.length < 1)){
    		UserName = prompt('UVA ID','');
    		UserName += '';
    		if ((UserName.substring(0,4) == 'null')||(UserName.length < 1)){
    			history.back();
    		}
    	}
    }
    
    function SendResults(Score){
    	var today = new Date;
    	var NewName = '' + today.getTime();
          var NewWin = window.open('', NewName, 'toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=yes,resizable=no,,width=400,height=300');
    
    //If user has prevented popups, no way to proceed -- exit
    	if (NewWin == null){
    		return;
    	}
    
    	NewWin.document.clear();
    	NewWin.document.open();
    	NewWin.document.write(ResultForm);
    	NewWin.document.close();
    	NewWin.document.Results.Score.value = Score + '%';
    	NewWin.document.Results.realname.value = UserName;
    	NewWin.document.Results.End_Time.value = (new Date()).toLocaleString();
    	NewWin.document.Results.Start_Time.value = StartTime;
    	NewWin.document.Results.submit();
    }
    
    
    
    //-->
    
    //]]>
    Code:
    <html><form name="frm1">
      <input type="text" name="timer1">
    </form></html>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    There is a much much easier way to do all that.

    First, just put the <form> right on the same page as the quiz. Notice the target= that I added.
    Code:
    <form id="Results" action="http://fp1.formmail.com/cgi-bin/fm192" method="post" target="sendResults">
    <input type="hidden" name="_pid" value="******">
    <input type="hidden" name="_fid" value="******">
    ...
    </form>
    Add an <iframe> to your page, thus:
    Code:
    <iframe name="sendResults" style="height: 1px; width: 1px; visibility: hidden;"></iframe>
    And then your code to send the form looks like this:
    Code:
    function SendResults(Score)
    {
        var form = document.getElementById("Results"); // named forms are obsolete; use id
        form.Score.value = Score + '%';
        form.realname.value = UserName;
        form.End_Time.value = (new Date()).toLocaleString();
        form.Start_Time.value = StartTime;
        form.submit();
    }
    Now you don't have to depend upon a popup working. So long as the browser supports <iframe>s (and no modern browser does not) it works.

    Now...

    If the submittal of the <form> results in something that you *WANT* the user to see, then you don't want a 100% hidden <iframe>, as I have it there, do you?

    Okay, simple enough:
    Code:
    <iframe id="sendResults" name="sendResults" style="height: 400px; width: 400px; display: none;"></iframe>
    Notice that now it has an id as well as a name. (That name is needed for the target= in the <form> tag.)

    And so, you just tack this line in at the end of the SendResults function:
    Code:
        document.getElementById("sendResults").style.display="block";
    }
    So that the <iframe> turns visible to display the results of the <form> posting.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,193
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    LOL! After doing all that, I realized that all you were really saying was that you want JavaScript to *calculate* the number of milliseconds from start to end, right?

    TRIVIAL!

    Code:
    var StartTime = new Date();
    
    ...
    
    function sendResults( )
    {
         var EndTime = new Date();
         var elapsedTime = EndTime.getTime() - StartTime.getTime(); // this *WILL* be in milliseconds!
    
         ...
         form.End_Time.value = EndTime.toLocaleString();
         form.Start_Time.value = StartTime.toLocaleString();
         form.Elapsed_Time.value = elapsedTime;   // add this field to your <form>
         ...
         form.submit();
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Huge Java Noob! Probably a stupid question, but argg....
    Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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