writing to the screen

They have: 6 posts

Joined: May 2000

....

[This message has been edited by Akim (edited 30 May 2000).]

They have: 117 posts

Joined: Feb 2000

Hi,

OK, I know this is probably a stupid question, but I can't get it to work, and I can't seem to find the answer anywhere.

How do I write a number variable to the screen? I know I can use:

document.write("hello");

to write text to the screen. But what if I have:

var x=10;

and I want to write it to the screen? I tried:

document.write(x);

but that did not work. Thanks for any help.

Bob

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Is 10 a string or a number?

If it's a string, you may want to have: var x="10"; and then document.write(x); should print: 10. But really, what you have it how it should work.

The only thing I can think of is to make sure your variable is declared (initialized) in the <head> tag or at least before you document.write to the page.

Also you may want check to make sure your tags around the document.write are correct.

<script language="JavaScript" type="text/javascript">
<!--

document.write(x);

//-->
</script>

AND, to make sure you have all your syntax correct. Capitalization and semi-colons, et cetera are the most common causes of JavaScript not working. It doesn't allow sloppiness.

hth,

Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

They have: 117 posts

Joined: Feb 2000

Suzanne,

Thanks for the help. I got it to work, somewhat, but I still keep getting errors that I can not figure out (this should be simple!). Here's the test code I'm using:

<html>
<body>
<head>
<script language="JavaScript" type="text/javascript">
<!--
var tot1=0;
var tot2=0;
function showit(){
tot1=tot1+1;
tot2=tot2+1;
document.write(tot1);
document.write(tot2);
document.close();
}
//-->
</script>
</head>
<form>
<input type="button" value="display results" onClick="showit()">
</form>
</body>
</html>

It writes a 1 to the screen for tot1, but then gives me an error saying "tot2 not defined". Any ideas what's wrong? Thanks again for your help.

Bob

They have: 117 posts

Joined: Feb 2000

Suzanne,

Just one minor correction to my last reply. I have to put a document.close after the first document.write to get it to display anything. The way it is in my post I just get the error message. Thanks again.

Bob

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Well, here goes nothing...

I have no idea what you are doing (I don't do enough JavaScript to guess, either), but here are my thoughts:

1. Why are you using tot1 = tot1 + 1; instead of tot1 = tot1++; ? (and same for tot2)

2. You need to print to something, I think. So if you have another field in your form (or another form?), and have the document.write to that field, you will get the results. I think.

Hopefully someone else can illuminate us both.

Suzanne

------------------
Zero Cattle
Suzanne
Tables DeMystified

They have: 117 posts

Joined: Feb 2000

Suzanne,

I'll try to explain what I'm doing. I maintain a web site for my local high school's drama organization.
http://members.aol.com/jproscenium

I used the site this year to allow the students to vote for their officers. I used a form with radio buttons that sent the results back to me in an email. I then tallied the votes manually. I'm trying to set something up to get the votes tallied automatically. I've been told on the forum that I can not do this with straight HTML, that I need to use CGI and/or Pearl. One suggestion was to use a guestbook, send the variables to the guestbook, increment, and write back out. I couldn't quite figure that out, but I did figure out that I could increment the guestbook with statements that increment a counter for each vote. In the end, the guestbook would contain a series of statements incrementing each variable once for each vote. I then just needed to download the guestbook and add some HTML to put those statements into a function, add a button to execute the function and display the final values for the variables. It actually seems to work, (not the best solution, I'm sure!). Also, I can now get the document.write to work, I just have to write out everything with one document.write statement.

I did try to use tot1=tot1++, but that did not increment the variable. Not sure why.

It seems like this should be a very simple thing to do, but I guess not. I may end up just using one of those canned POLL programs, but I wanted more control myself, and I wanted to avoid the advertising they come with.

Sorry for the long post. Thanks again.

Bob

They have: 231 posts

Joined: Feb 2000

Your going about this thing the wrong way. You dont use a guestbook to gather data and tally results. A guestbook used to show and write messages. Definetely not used to retrieve variables. You CANNOT do this alone with JavaScript. Have you tried posting in the Perl & CGI Forum or the Misc. Scripting Forum??? If your host supports ASP I will make one for you.

:: Lloyd Hassell :: http://www14.brinkster.com/lloydh ::

They have: 117 posts

Joined: Feb 2000

Lloyd,

I do realize that I'm going about this the wrong way, and that I can not do it with just Javascript. I know that a guestbook is not meant to do what I'm trying to do - in fact I have a guestbook set up on my site for the correct purpose. I'm only trying it because someone on the Forum suggested it, and I haven't been able to figure anything else out. I did post to the Perl/CGI forum. I got one suggestion, but I did not understand it enough to do anything with it.
My host is AOL. I don't know if they support ASP. In fact, I'm not even sure what ASP is. If you can help me with this, I'd be very grateful, but I really don't expect you to go to that much trouble.

By the way, let me just add a thanks for all the work you do in moderating this forum. It's been very beneficial to me in answering many questions, and I've learned a lot just by reading other peoples questions and answers. Thanks!

Bob

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Bob,

Lloyd: I may have been the one to suggest the guestbook. Someone else [http://members.aol.com/justYaz] at Almost OnLine has managed to use his guestbook as a forum, which in itself is a feat. If I recall properly Bob was trying to find a purely javascript solution.
Bob: Aol does not support anything (other than itself) ASP -- active server pages -- for the most part vbscript.
Regarding accessing the previous values: If I remember right AOL's guestbook automatically increments some sort of guestbook post number -- you should be able to access that. If not, your polling form could direct the individual to another page that has an odometer (number of visitors) which you can use for your numbers. Sort of like:
< input type='button' value='doe/vote1.html'>John Doe
< input type='button' value='smith/vote2.html'>Jane Smith

put an odometer on each page and each page in its own directory.

Yes Lloyd, I know the above is a kludge and a bad one at that -- but, so is AOL (and I know because I'm stuck with it [kids]}.

Vinny

------------------
GrassBlade: cut&paste javascript
The Javascript Place Forums

Where the world once stood
the blades of grass cut me still

They have: 117 posts

Joined: Feb 2000

Vinny,

I actually was able to get this to work using your idea of using a guestbook. I agree with you and Lloyd - it's an incredible kluge.

With each vote, I added lines of javascript code to the guestbook that check for which choice was voted for and increments the apporpriate variable. This code is within a function. At any point, I can download the guestbook source and append to it some more code to print to the screen the value of each variable, along with a button to execute the function. I then upload the code, go to that page, click the button, and it prints the values of each variable to the screen.

It actually works, but is definitely NOT the best solution!

I guess I need to get to a CGI/Perl class and get off AOL if I really want to get deeper into this stuff!

Thanks again,

Bob

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi Bob,

We all need to get off Almost OnLine. (Anyone out there want to adopt some kids? )

Vinny

------------------
GrassBlade: cut&paste javascript
The Javascript Place Forums

Where the world once stood
the blades of grass cut me still

They have: 231 posts

Joined: Feb 2000

Bob: If you use AOL then you will not be able to use ASP scripting. ASP is a server-side language which allows you to write text files (which JavaScript can't) and many other things too. It is similar to Perl but uses a different programming language and server.

I suggest you go to www.freepolls.com and use their service. They have exactly what you are after.

:: Lloyd Hassell :: http://www14.brinkster.com/lloydh ::

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.