PDA

View Full Version : Help with simple ASP page for school?


Iconoclast
02-11-2009, 02:30 AM
Hey guys, I have to create a fairly simple ASP page for an assignment for my web management class and I seem to have run into a wall. I know the basics about ASP, and I am continuing to learn through W3Schools more advanced concepts. However, I can't seem to put together the knowledge that I have in order to complete this assignment as instructed.

Here is the task I have to complete:
Create a page that accepts the following information:
-10 different lab marks
-2 different project marks
-2 different test marks
-submit button
-reset button

Next, use VBScript to:
-when the submit button is clicked, store each value from the form into an array
-loop through the array
-add marks for labs
-add marks for projects
-add marks for tests
-output on the page: overall lab mark, project mark, and test mark
-use a nested if statement or select case statement to output the letter grade for the student's overall mark


Now, the part I'm having trouble with at the moment is how to store the values from the form textboxes in an array. We did a very similar project to this several weeks ago, but using JavaScript instead of ASP/VBScript. I know they are both similar, and perhaps someone can explain to me what differences there may be.

So far, my main body code looks like this (the red code is unfinished, as it is the part I need help with):
<%
dim i
for i=1 to 14
%>


<form name="lab2" method="post" action="lab2.asp">

<input type="text" name="lab1mark" />
<input type="text" name="lab2mark" />
<input type="text" name="lab3mark" />
<input type="text" name="lab4mark" />
<input type="text" name="lab5mark" />
<input type="text" name="lab6mark" />
<input type="text" name="lab7mark" />
<input type="text" name="lab8mark" />
<input type="text" name="lab9mark" />
<input type="text" name="lab10mark" />

<input type="text" name="project1mark" />
<input type="text" name="project2mark" />

<input type="text" name="test1mark" />
<input type="text" name="test2mark" />

<input type="submit" name="submit" value="Submit" />
<input type="reset" name="reset" value="Reset" />


<input type="text" name="outputlab" readonly="readonly" />
<input type="text" name="outputproject" readonly="readonly" />
<input type="text" name="outputtest" readonly="readonly" />

<input type="text" name="outputletter" readonly="readonly" />

</form>

I know the formatting isn't anything great, but I'm not concerned about that at this point (I want to get the code itself working properly before I deal with how it looks).

Now, as I mentioned above, we did a similar thing to this in a previous project in which we needed to populate the options of a drop-down menu from a JavaScript array. I'm not sure if this will be of any help, but I hope it might be:
function addOption(selectbox,text,value )

{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

function addOption_list(selectbox)
{
var comicBooks = new Array("Batman","Spiderman","Hulk","X-Men","Superman");
for (var i=0; i < comicBooks.length;++i)
{
addOption(document.myForm.comicList, comicBooks[i], comicBooks[i]);
}
}


Can anyone help me out with this? I'd really appreciate it! :)

Spudhead
02-11-2009, 09:59 AM
Ok... helping with homework isn't, strictly speaking, adhering to the posting guidelines. But FWIW:

Take the red bit out. You don't need to do any looping on the form page, just create a static form with all the required input boxes on it.

All your VBScript goes on the page that your form submits to - and here's where I have a problem with your assignment: I cannot see any justification for putting those form values into an array. You already have them in the Request.Form collection. All you need to do is loop through that (http://www.devguru.com/technologies/asp/9141.asp), adding their values to each total.

Iconoclast
02-12-2009, 03:52 AM
Take the red bit out. You don't need to do any looping on the form page, just create a static form with all the required input boxes on it.
Oh ok, I was under the impression the code had to go in the page with the form, but now that I think about what you said, it definitely makes more sense for it to be in the submit page :)

All your VBScript goes on the page that your form submits to - and here's where I have a problem with your assignment: I cannot see any justification for putting those form values into an array. You already have them in the Request.Form collection. All you need to do is loop through that (http://www.devguru.com/technologies/asp/9141.asp), adding their values to each total.
I didn't know all values were held within that collection, this will provide me with several new ideas to try.

Thanks for the tips! :D

Iconoclast
02-12-2009, 08:19 AM
Ok, I have done some more work and now have another question. I have created my form with inputs and buttons on my HTML page, using the POST method and setting the action to my "lab2.asp" page.

In my lab2.asp page, this is the code I have made:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Lab 2 Results</title>
</head>

<body>


<%
dim l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, p1, p2, t1, t2
dim l, p, t, total
l1 = Request.Form("l1")
l2 = Request.Form("l2")
l3 = Request.Form("l3")
l4 = Request.Form("l4")
l5 = Request.Form("l5")
l6 = Request.Form("l6")
l7 = Request.Form("l7")
l8 = Request.Form("l8")
l9 = Request.Form("l9")
l10 = Request.Form("l10")
p1 = Request.Form("p1")
p2 = Request.Form("p2")
t1 = Request.Form("t1")
t2 = Request.Form("t2")
l = (l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9 + l10) / 10
p = (p1 + p2) / 2
t = (t1 + t2) / 2
total = (l + p + t) / 3
Response.Write("Overall Lab Mark: " & l & "<br />")
Response.Write("Overall Project Mark: " & p & "<br />")
Response.Write("Overall Test Mark: " & t & "<br />")
If total >= 101 Then
Response.Write("Oh noes, you've made a mistake!")
ElseIf 100 > total > 80 Then
Response.Write("Overall Mark: A")
ElseIf 79 > total > 70 Then
Response.Write("Overall Mark: B")
ElseIf 69 > total > 60 Then
Response.Write("Overall Mark: C")
ElseIf 59 > total > 50 Then
Response.Write("Overall Mark: D")
ElseIf total < 50 Then
Response.Write("Overall Mark: F")
%>


</body>
</html>


Basically, it declares variables for each input element and assigns them...then totals them together as necessary.

However, when I enter values into my HTML form page and click Submit, it brings me to the lab2.asp page, displaying exactly what I have posted above (the straight ASP code). What am I doing wrong and how to I make it output the results like they should? (PS - I used this tutorial (http://www.w3schools.com/asp/showasp.asp?filename=demo_simpleform) with regard to the POST method).

Also, another question I have (relating to If...Then statement I have used at the end of my ASP code)...this is what I want to happen.

If the total mark is greater than 100, then display an error.
If the total mark is between 80-100, then assign an A grade.
If the total mark is between 70-79, then assign B grade.
If the total mark is between 60-69, then assign C grade.
If the total mark is between 50-59, then assign D grade.
If the total make is below 50, then assign F grade.

However, while I know how to make a basic If statement (If x > y), but I wasn't sure how to make an If statement that would determine whether x is between two different specific values. What I did was my best guess, but I'm not sure if it's correct (and I can't check to see if it works until I resolve the first issue I mentioned).

Any help would be greatly appreciated! :D

Old Pedant
02-13-2009, 06:21 AM
Well, I do *NOT* think you are following the instructions given to you.

You aren't looping. You aren't using an array (let alone 3 arrays.)

Having said that, let's just look at the truely egregious errors you made.

(1) Values from Request.Form and Request.QueryString are *ALWAYS* strings!

t1 = Request.Form("t1")
t2 = Request.Form("t2")
...
t = (t1 + t2) / 2

So if the form field t1 held 72 and the form field t2 held 60 then when you do t1 + t2 you will *ACTUALLY* be APPENDING the two strings "72" an "60" for a result of "7260". When you then do the divide-by-two, VBScript will *THEN* decide that it needs to convert the dividend from a string into a number, so you will then do 7260/2 and get an answer of 3630. Not quite what you were expecting!

You must *ALWAYS* convert your request values to numbers if they are supposed to be numbers.

HINT: Look into the CDBL( ) function.
HINT: What do you do if the user types in "aardvark" where you are expecting a number?

********************

(2) You can NOT string multiple comparisons together. This is true in every computer language I know of.

ElseIf 100 > total > 80 Then

No way. You would need to write that as

Elseif 100 > total AND total > 80 Then

Or would you??? Think hard about the fact that you are using ELSEif. You already made the test for total >= 101, didn't you? Why do you need to test for total < 100 again???

But your logic there is also flawed in two other ways:
(1) You have assumed that total will be an integer. What happens if it is 100.3????
(2) You completely omitted the PERFECTLY LEGAL score of exactly 100.

***************

Again, if your instructor required that you use an array, you have failed the question, in my opinion. Were I grading you, you wouldn't get more than 50% to 60% even if every other aspect of your answer was correct.

Iconoclast
02-13-2009, 07:58 AM
Well, I'm not sure whether I should be thanking you or commenting on the contemptuous tone you expressed it in.

First of all, I'm merely asking for help (fairly politely, might I add) with something that I do not understand. Forgive me for not having the same level of knowledge about this subject as you may have, but as far as I'm concerned that's what these forums are for, and if you can't offer support without being a pompous jackass then I would suggest not bothering with it at all.

Second of all, while you've commented on my lack of use of arrays (which, I'm sure, had you taken the time to think about it, you would have realized I also do not yet fully understand), you have failed to mention how I might go about incorporating one (or three) into my project. So in that regard, feel lucky that you are not the one responsible for grading me on this.

Now, thank you for informing me of my errors with the form input strings. I was unaware that it worked in that manor, and now I can make revisions as necessary.

I also appreciate you pointing out the logic of my If...Then statement, for after reading your comments it now makes sense to me the structure I need to use.

Snide remarks aside, I appreciate your ideas on what I have done wrong. Any more constructive input will be greatly appreciated and undoubtedly useful.

brazenskies
02-13-2009, 01:12 PM
out of all the horses on the forum, his is definately the highest!

Iconoclast
02-14-2009, 06:33 AM
Ok, I've been reading up some more on ASP arrays and looping, and I think now I have a much better idea of what I need to do.

However, I'm still running into the issue of when I click the Submit button, it simply brings me to my .asp page, and displays the straight unparsed code. I installed IIS7 (I'm running Vista Home Premium), thinking this would solve the issue...yet it persists. Can anyone tell me why and how to fix it? Thanks! :)

Old Pedant
02-14-2009, 08:20 PM
*SIGH* I was trying to HELP you. I was pointing out that though you COULD get this code working to the point of displaying right answers (mostly by fixing your comparisons). But if this is truly for a programming class, then I can't see that the instructor can give you a lot of credit for answering the question the wrong way.

And if you read the rules of this forum, you will see that we are *NOT* supposed to answer homework questions here! Look at this page:
http://www.codingforums.com/rules.htm
See Rule 1.5

I really felt that I was helping you as much as I should. If others want to violate that rule and risk getting banned from the forums, I guess that's their choice. I haven't been here long enough to know how flexible those rules really are.

I guess I'll take a chance and show you how you can do the "grading" more compactly. One possible way:

mark = "F" ' assume the worst
If total > 100 Then
Response.Write "Impossible score of " & total & ", error."
Response.End
Elseif total >= 80 Then
mark = "A"
Elseif total >= 70 Then
mark = "B"
Elseif total >= 60 Then
mark = "C"
Elseif total >= 50 Then
mark = "D"
End If
Response.Write "Mark is " & mark & "<p>"
...
%>

Do you see why you don't have to keep testing total for being less than the prior boundary?? You won't have REACHED the ELSEIF line if the total is higher! For example, you won't reach the
Elseif total >= 50 Then
if the total is 60 or above, because the prior if's have taken care of that. That's the real beauty of ELSEIF in place of a series of separate IF...THEN...END IF tests.

***************

Regarding IIS and ASP: I don't have IIS 7, but I've read this question dozens and dozens of times in various forums. You have to specifically enable ASP with IIS 7. It does not come enabled by default (MS has some sort of reason for this; not clear to me that it's a good reason, but whatever). If you Google for "enable asp on iis7" you'll see that it's so popular a question that Google has it pre-indexed.

Iconoclast
02-14-2009, 08:52 PM
Yes, once you explained the logic for the ElseIf I was able to revise it with essentially the same thing you put in your last post, I'm not exactly sure why I didn't realize that to begin with =/

I went back to my IIS installation and this time made sure to enable ASP as well as a few others, but it still seems to be giving me the same issue.

Does it have anything to do with the location the file(s) are in? For example, I know when you install IIS you get the C:\inetpub\www\ folder, where you can add your own custom homepage, etc...and one of my buddies mentioned something about a virtual directory, but I don't believe it has anything to do with that (correct me if I'm wrong). For example, I only have two files: my index.htm file, and my lab2.asp file, both of which I am running off of my USB stick (where I keep the rest of my schoolwork). Do I have to run them from within the IIS manager or something, or does that matter? Thanks!


EDIT: Upon reading on ASP arrays and looping and came across this example...however I'm a bit confused by it, and wondering if anyone can explain it to me in more detail:

You can loop through all the values in a form request. If a user filled out a form by specifying two values - Blue and Green - for the color element, you could retrieve those values like this:

<%
for i=1 to Request.Form("color").Count
Response.Write(Request.Form("color")(i) & "<br />")
next
%>

Output:

Blue
Green

First of all, in the second line (following the <%), it has Request.Form("color")...What exactly is color in this sense? Is it the name of the form?

Because my HTML form has 14 different textboxes, all of which that have different names...how do you loop through all of the different textboxes with one loop even though the names are all different?

If my textboxes are named l1, l2, l3, etc. respectively for each lab textbox, and p1, p2, and t1, t2 for each project and test textbox respectively, then how would I loop through those elements with the Request.Form object? I spent quite some time trying to figure this out but so far it still remains a question to me.

Someone else mentioned I could use something like this for a loop:

<%
dim myArray(13)
for i = 0 to 9
myArray(i) = trim(request.Form("lab" & (i + 1) & "mark"))
next

But I'm still confused...first of all, what does the "trim" mean? (I'm assuming it's like the truncate, will cut off any decimals after a whole integer?)

Old Pedant
02-14-2009, 09:19 PM
Regarding the location of your ASP/HTML files: They *CAN* be anywhere on any drive, but if they are not in C:\inetpub\wwwroot then you have to create a virtual directory that points to them. You can do that in XP from the IIS control panel, but not sure if it's different in Vista. I would hesitate to guess. Sorry.

****************

Yes, you have the right idea here:
dim myArray(13)
for i = 0 to 9
myArray(i) = trim(request.Form("lab" & (i + 1) & "mark"))
Next

Comments: There's really no reason to mess with the difference between element zero of the array and "lab1mark". Not worth the hassle. Just leave element zero of the array empty and loop from 1 to 10 or even 1 to 14 (why not just have lab11mark, lab12mark, etc., with the implication that 11/12 and 13/14 are not ordinary marks?)

TRIM( ) is a *STRING* trimmer. It removes any space on both ends of the input value. It's useful for the common error that people make of hitting a space key before or after an entry.

This code still has the bug that it is storing *STRINGS* into the array elements. Remember what I said: Request.Form(xxx) and Request.QueryString(xxx) *always* return strings. (Well, and TRIM( ) always returns a string, as well.) You really should convert those strings to numbers before storing the values in the array. I mentioned CDBL( ) before. But warning: That will give you an error (and stop the program) if the value from the form is *NOT* a number. That's probably okay for a beginning program, but keep it in mind. HINT: There is also a ISNUMERIC( ) function that will test if a string is a number or not.

Listen: If you picked up on the right way to do ELSEIF from my earlier comments, you're doing fine. You'll get all this. And if you indeed use the array (or arrays...not clear to me which is better for this), you should score fine. So if I sound curmudgeonly, my apologies. But I really WOULD like to see you score well.

FINALLY... One thing that will help you TREMENDOUSLY is to DOWLOAD the "Windows Script 5.6 Documentation" from here:
http://www.microsoft.com/downloads/details.aspx?familyid=01592C48-207D-4BE1-8A76-1C4099D7BBB9&displaylang=en

It's a compiled HELP file with a *COMPLETE* index and a usable table of contents and more. You can get all the help you need from it for functions such as CDBL( ) and ISNUMERIC( ) and TRIM( ) and more.

Good luck.

Basscyst
02-19-2009, 12:03 AM
I just want to pop in and say that this is completely appropriate discussion in terms of help with homework. The OP is just asking for help, not for work to be done for him / her.