![]() |
|
|
Published: Monday, November 22, 1999 By Marc Draco If you're new to ASP and HTML and you want to make your website that little bit more personal this might be the place to start.
I first started mucking around with ASP because a friend of mine paid in excess of $2000 for a website and it was a mess. It was a hash of lousy design and didn't do a thing for his image: as it stands now, only you can be the judge. This article describes how to create on-line quizes and parse response forms without benefit of a database or the CDO to perform the email - more on that later. If you want a really cool database-based quiz, you should read Ryan S.'s great article on creating On-Line Quizes, but if you like it low-down quick and dirty, then here's the place you should be! A real-life version can be seen working at: http://www.nima99.co.uk/quiz.asp. Just don't shoot me - I did write the questions! For the sake of example, we'll stick to something simple with just three variations on the questionaire and a self-marking quiz with three questions. The orginal uses tables to format the text but that clutters the listings, which are more important here. There are three different inputs a textbox, a radio group and a dropdown list. In real-life you can use checkboxes and even multiple selctions depending on what your form needs to do. For speed, I design everything using FrontPage Express or Netscape Composer (they're free and I'm cheap) and add the code using David Wier's superb ASP Express. This ecclectic approach will be frowned upon by purists but it allows for very fast development. The crucial factor to to making any form based upon this approach is in naming the objects. Each entry must be "named" in numerical sequence; ideally starting from 1. The end-user doesn't see this so it won't matter to them, but the parser relies on it. I.e. all the objects in Entryfield 1 are named "1"; Entryfield 2, "2" and so on. You'll see this in action in Listing 1.
I should probably mention at this point that it is possible to use a
There are a couple of problems with this approach. Not least that you have to use POST (GET doesn't work). But in any case the data that is attached to the email gets scrambled so badly even the humble space is replaced by "+". Although that's not difficult to overcome, Netscape and Explorer both warn the user they are sending form contents by email with a painful modal request. Awww poo.
SKINNING CATS In reality reusable code like this should be Included where needed. (If you don't know how to use the Include directive try this great tutorial on 4Guys.) So let's look at a really simple example, Listing 2 and Listing 3. Most of the code in Listing 3 should be fairly obvious as it relies on the fact that we know how many questions are involved (three in our example) so we set a variable to that; OK, for purists, it's a constant. Next we'll pluck the player's name from the form; since this is not a question we don't assign it a number and if nothing's supplied, we give it a simple default. If it was really important that you had that information, a simple popup should suffice and there are several articles on 4Guys discussing form validation and popup warnings.
Next up, we use
Now we're going to email those results back to the webmaster. Now I gather there is some contention regarding the implementaion of
MailTo: doesn't work;(b) Don't have a newer browser with MailTo: support;(c) Can't spell thier own email; (d) All of the above. Problems (a) and (b) can be circumvented with the optional Collaborative Data Object, which can be used to send an email without going through a client's own email applet. There's a smashing article on 4Guys which discusses it in more depth but I haven't used it here simply because the server hosting this example doesn't have the required SMTP installed yet (see the article Sending Emails in ASP Using CDO for more information on using CDO)! However, the code presented here should convert to the CDO in minutes. Problems (c) and (d) are always going to be around and those users would be better off sticking with pen and paper. Constructing the actual hyperlink is worth study since it takes place over several lines of code. The first generates the hyperlink proper with the recipient's address plus the optional body tag. Then, a For...Next loop steps through the form data again and generates the body of the email. Finally a bit of straight HTML closes the tag and cleans up. Done in this way, we have to separate each line of the answers with a delimiter (\) because the tag does not support multiple lines. However, this is easier to read at the other end than a form-based attachment and the user doesn't get pestered with a modal alert about security.
CAT SKINNING Part Deux:
Two different solutions come to mind that may suit different situations. The first is uses a delimiter in the
Now our decoder code looks more like Listing 4. A similar loop and test would be added to the hyperlink construction later.
However, this is a little clumsy since ASP already allows us to count the number of fields in a form using
Two B, Or Not Two B What's important to note here is the way in which the answers are tucked in the form along with the questions! For example:
Now I can just imagine you thinking that I've gone quite mad: but wait - this is a fun quiz and no one would be looking at the source unless they wanted to cheat. In itself, that would be pointless since the quiz will correct you at the end anyway. Once again, in order for this parser to work, the inputs must be named correctly. Hidden inputs (which don't have to follow the questions, by the way) must be named as shown. That is: answer 1 is 1a, answer 2 is 2a and so on. The format of the value part for each is in two parts. A single character represents the correct option (A, B, C, etc.) followed immediately by a text version of the correct answer - which could be a short explanatory paragraph if required, e.g.:
And if you're really enthusiastic, the tag can even contain HTML markup.
Neat eh? Before you get too carried away though, as the code stands the "explanation" is only displayed if the user selects full scoring and makes an error on that question. Ok, over to Listing 7: here's the tricky bit.
Constructs the question number + "a" - i.e. "1a" and places that "hidden" form response into
Gets the explanatory text by selecting everything but the first character, and finally:
Takes just the first character from the string and places it in The remainder of this code should be fairly self-explanatory. That concludes this discussion - if you do use it to impliement your own quiz, drop me an email so I can have a go at it. Happy Programming!
Attachments:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||