Forum Moderators: open
Part 3 :: remming and extending variable requesting ::
1. Remming - So long as it's outside a command one apostrophe can rem a whole line. Short and simple:
Remming is a posh term for note taking inside a language such as ASP itself. Different languages have different methods to identify a rem. PHP's is // CGI's is # and ASP's is '
' Displays cool if cheesy has more than 8 characters
If Len("cheesy")>=9 Then
response.write "cool"
End If
response.write "Is cool there?" 'Displays question ASP ignores the lines which have ' at the beginning and all text from the ' but not code before. Remming is useful to display notes on commands for which you may forget their point.
2. Extending Variable Requesting - Handling A Subscribe Form In A Basic Way:
On some pages especially those requesting personal information, you may have seen pages like this:
http://example.com/subscribe.asp?firstname=Joe&surname=Bloggs&email=joebloggs@example.com&username=joebloggs Suprisingly enough, you can handle that information from what I previously taught you:
If Len(request("firstname"))>=3 Then
response.write "Hello " & request("firstname")
End If This looks complex but it covers everything we've learnt so far. Let's go over it step by step.The first line introduces Len(), telling it that it has to recognize character length, the next function tells it that it has to request() information and in the speechmarks tells it what it has to request [ firstname ] and if what has been requested is 3 or over in character length, it has to write Hello and then the firstname the user entered in the box. Then it ends if. And after that you create a new If handling the surname and the email etc.
It checks to see whether the firstname is valid, in short, of course someone could enter Jo, which is perfectly valid and get rejected. Don't use it. It's an example!
3. You Have A Go:
What is wrong with this code?
If cint(request("number"))<=0 Then
response.write "You have a valid number. It is " & request("number")
Else Then
response.write "This number is not valid. Your invalid number is " & request("number")
End If Remember:
1. In order to rem it has to be outside a function
2. Remming is done by a ' in ASP.
3. Len is used for checking character length, cint is used for checking numeric value
5. ElseIf has no space between eachother but End If does.
6. All asp commands must have <% %> around them
7. All pages containing asp must have a .asp extension
Previous Tutorials:
Response and Request [webmasterworld.com]
If, Cint And Len [webmasterworld.com]
ASP Links:
Variables :: A vital part of any Sever Sided Language [w3schools.com]
ASP With W3Schools [w3schools.org]
HTML Goodies :: Beyond :: ASP [htmlgoodies.com]
Hope you enjoyed reading my tutorials and good luck!
cmatcme
Answer: Else shouldn't have Then after it
Section 2.
If Len(request("firstname"))>=3 Then
response.write "Hello " & request("firstname")
End If
Your code is a very bad example to set for beginners. Try this...
<%
Dim RqFirstname
RqFirstname = CStr(Request.Querystring("firstname"))If Len(RqFirstname) > 2 Then
Response.Write("Hello " & RqFirstname)
End If
%>
The correct way of accessing items in the Querystring is with Request.Querystring. If you do not specify which collection you are accessing, it makes your code harder to read. Also, it could suddenly break if later you amend your code to use Request.Form on the same page.
You should declare a local variable to use in place of the Request object if you need to access the information more than once. Local variables are much faster to access than items in the Request object.
Use standard VBScript coding conventions (namely capitalise the first letter of keywords). This helps other developers who might read your code to more easily understand it at a glance.
In order to rem it has to be outside a function
You can use comments inside your functions as well. In fact if you're programming your pages well, you should have very little code outside of functions so most of your comments will be inside the functions.
Remming is done by a ' in ASP
You are confusing ASP with VBScript. They are two very different things.
If you are using VBScript with ASP, then yes, you can comment your code with an apostrophe ( ' ), and also with the Rem Statement.
However...
If you are using JScript, you can comment your code with // or /* */
If you are using Perlscript, you can comment using #
Len is used for checking character length, cint is used for checking numeric value
The Len function returns the length of a string.
The CInt function converts one data type (eg a Long or a String) in to an Integer. eg.
<%
Dim MyString, MyInteger
MyString = "1233.76"
MyInteger = CInt(MyString)
Response.Write(MyInteger)
%>
This will display: 1234
All asp commands must have <% %> around them
All ASP code must be contained between eitheier
<% and %> bracket
or
<script RunAt="server"> </script> tags.
eg:
<script Language="JScript" RunAt="Server">
function WriteHelloWorld()
{
Response.write('Hello World!');
}
</script>
<script Language="VBScript" RunAt="Server">
WriteHelloWorld()
</script>
Both of those script blocks can appear in the same page. Please remember to use the RunAt="server" attribute as otherwise your code won't run on the server, it will be sent to the browser.
Note that The script tags are the only way to run two seperate languages on the same page within ASP. This can not be done within the <% %> tags.
All pages containing asp must have a .asp extension
ASP can only be run from files whose which have been mapped to asp.dll in IIS. By default, this includes .asp files and .asa files. However, if you wish, you can set IIS to map other extensions. For example, you could map .html files to asp.dll and run ASP code within those files!
It looks like you still have a lot of learning to do yourself cmatcme ;-)