Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-23-2012, 08:41 AM   PM User | #1
Spency
New to the CF scene

 
Join Date: Nov 2012
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Spency is an unknown quantity at this point
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>
Spency is offline   Reply With Quote
Old 11-23-2012, 08:23 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,575
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 11-23-2012, 08:30 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,575
Thanks: 62
Thanked 4,062 Times in 4,031 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 11-24-2012, 08:11 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:52 AM.


Advertisement
Log in to turn off these ads.