Wednesday, 30 November 2011

PASS VARIABLES WITH FORM

Ok, so you want to pass variables between your web pages. Good news, it's a piece of cake with ASP. As with just about everything else in ASP, there are many ways to do this. In this tutorial, we will show you how to pass variables from one page to another using a simple form.

Let's say you have a contact form on your website and you want to personalize the confirmation page for the user. The first thing you need to do is create a contact.asp page and paste the code below into your page:


Contact Us


Email
First Name:
Last Name:
Subject:
Comments:




Now, you've got your basic form. Simple enough, right? Next, create a contactconfirm.asp page and paste the code below:

<% DIM strEmail, strFirstName, strLastName, strSubject strEmail = Request.Form("Email") strFirstName = Request.Form("FirstName") strLastName = Request.Form("LastName") strSubject = Request.Form("Subject") %>


Confirmation


<% Response.Write strFirstName %>,

Thank you for contacting us. We have received your message and will send a reply to your email address, <% Response.Write strEmail %>, as soon as possible.




This is a nice way to personalize the users experience by displaying the first name and email address that they entered on the form page.

When passing variables with a form, the Request.Form initiates the request for the form data and the ("Email") specifies the form field we are requesting. In this case, we created our own variables: strEmail, strFirstName, etc and set their values equal to the data from their respective form field. Rather than creating your own variables and setting each of their values, you could simply place <% Request.Form("Email") %> directly in your confirmation page instead of the <% Response.Write strEmail %>. Again, this is a matter of personal preference. However, we recommend you use the variable technique, especially when you start dealing with more complex ASP pages.

Now, you can pass variables from page to page within your website. Piece of cake!

f you want to learn another way to pass variables between pages, be sure to check out our Passing Variables with QueryString Tutorial.

http://www.aspwebpro.com/tutorials/asp/passvariableswform.asp

PASS VARIABLES WITH FORM

Ok, so you want to pass variables between your web pages. Good news, it's a piece of cake with ASP. As with just about everything else in ASP, there are many ways to do this. In this tutorial, we will show you how to pass variables from one page to another using a simple form.

Let's say you have a contact form on your website and you want to personalize the confirmation page for the user. The first thing you need to do is create a contact.asp page and paste the code below into your page:


Contact Us


Email
First Name:
Last Name:
Subject:
Comments:




Now, you've got your basic form. Simple enough, right? Next, create a contactconfirm.asp page and paste the code below:

<% DIM strEmail, strFirstName, strLastName, strSubject strEmail = Request.Form("Email") strFirstName = Request.Form("FirstName") strLastName = Request.Form("LastName") strSubject = Request.Form("Subject") %>


Confirmation


<% Response.Write strFirstName %>,

Thank you for contacting us. We have received your message and will send a reply to your email address, <% Response.Write strEmail %>, as soon as possible.




This is a nice way to personalize the users experience by displaying the first name and email address that they entered on the form page.

When passing variables with a form, the Request.Form initiates the request for the form data and the ("Email") specifies the form field we are requesting. In this case, we created our own variables: strEmail, strFirstName, etc and set their values equal to the data from their respective form field. Rather than creating your own variables and setting each of their values, you could simply place <% Request.Form("Email") %> directly in your confirmation page instead of the <% Response.Write strEmail %>. Again, this is a matter of personal preference. However, we recommend you use the variable technique, especially when you start dealing with more complex ASP pages.

Now, you can pass variables from page to page within your website. Piece of cake!

f you want to learn another way to pass variables between pages, be sure to check out our Passing Variables with QueryString Tutorial.

http://www.aspwebpro.com/tutorials/asp/passvariableswform.asp

NEW WAY TO HANDLE VARIABLES

The EASIEST way to deal with variables!!

Allow me to introduce myself.

My name is Rob Collyer, I lay eyes upon ASP years ago and have been heavily coding since.
It always struck me as a complete pain in the neck the way in which we have all been dealing with variables in ASP, for as long as we have used ASP.

What do I mean? ........ Allow me to explain with this example of the hard way:-

<% FirstName = Request.Form("FirstName") Surname = Request.Form("Surname") Address1 = Request.Form("Address1") Address2 = Request.Fomr(Address2") ... ... %>

Rather than go on longer than necessary into yet more lines looking exactly the same as the ones above, to put my point across, I'll put you out of your misery.

Why do variables have to be this TEDIOUS to deal with??? I wanted a way to read in all variables at once in one go, with just one function call.... I must be mad.... sure enough, I was told this as well as things like 'It's not possible', etc.

Sure enough, at the time, it wasn't possible. To my rescue came Microsoft, with Version 5 of the VBScript engine and a new command called Execute. Execute(string) basically executes a string as though it were ASP.. So Execute("Response.write ""Hello"") .... would have the same effect as just using response.write "hello"

As soon as I saw this new feature I knew instantly what I could do with it:-

<% For Each Field in Request.Form TheString = Field & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>

We are setting variables automatically. That one little tiny piece of code, has saved me so much time over the last year, you will not believe.
Ok, it does have it flaws, Forms with image submit buttons pass form variable names like "submit.X", and "submit.Y" (Co-ordinates where you clicked on the submit image) which obviously wont do for variable names in ASP, and results in the white error page from hell.... But we can start to prevent these things:-

<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops TheString = tmpField & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>

Why stop there?? There are many silly characters that are fine in HTML controls as names, but when setting variable names automatically like this in VBScript, these characters will not do. You can easily add additional lines as above to escape other characters like spaces, hyphens and all manner of other ones too.

You've got multiple controls on forms.... how are you gonna assign variables to multiple select boxes?
As a developer, I found it handy to read these 'multiple' part form variables into an array... I will build upon the above code to handle multiple items and create arrays from them:-

<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops ItemCount=Request.Form(Field).count IF ItemCount > 1 then
execute "redim " & field & "(" & itemcount -1 & ")" 'Dynamically dimension the array
For Item = 0 To ItemCount - 1
TheString = tmpField & "("&Item&")= request.form(""" & field & """).item(" & Item + 1& ")"
Next
Else
TheString = tmpField & "= Request.Form(""" & Field & """)"
End If
Execute(TheString)
Next
%>

I hope by now you can see the potential of what all of the above means to you as the developer.....
I urge you all to write your own functions that'll handle variables the way you want them to, and please remember.... FORMS were just an example, don't stop there, after all there are Querystrings, Cookies, Session variables, Application Variables, ServerVariables. Then there are recordsets (why not?) and the dictionary objects, etc, etc, etc.

Just remember, you are now armed with info which will save you a great deal of time, why not get busy writing some variable handling functions of your own??

If anybody wants a function to selectively handle Form, Querystring, Cookies, Session, Application and Server variables then I have one...just mail me. It also has a parameter to run in DEBUG mode which will show you variables in all objects, their values and whether their names present a problem during conversion. It'll also flag those controls that can be set to arrays, etc.

Anyway, those that do feel inspired to get writing their own handlers, let me ask one thing.... Send me a copy of your finished function(s), this is pretty new stuff and I'm well open to being inspired by YOUR code.

Feel free to contact me with your feedback.

Enjoy!
http://www.aspwebpro.com/tutorials/asp/newwaytohandlevar.asp

NEW WAY TO HANDLE VARIABLES

The EASIEST way to deal with variables!!

Allow me to introduce myself.

My name is Rob Collyer, I lay eyes upon ASP years ago and have been heavily coding since.
It always struck me as a complete pain in the neck the way in which we have all been dealing with variables in ASP, for as long as we have used ASP.

What do I mean? ........ Allow me to explain with this example of the hard way:-

<% FirstName = Request.Form("FirstName") Surname = Request.Form("Surname") Address1 = Request.Form("Address1") Address2 = Request.Fomr(Address2") ... ... %>

Rather than go on longer than necessary into yet more lines looking exactly the same as the ones above, to put my point across, I'll put you out of your misery.

Why do variables have to be this TEDIOUS to deal with??? I wanted a way to read in all variables at once in one go, with just one function call.... I must be mad.... sure enough, I was told this as well as things like 'It's not possible', etc.

Sure enough, at the time, it wasn't possible. To my rescue came Microsoft, with Version 5 of the VBScript engine and a new command called Execute. Execute(string) basically executes a string as though it were ASP.. So Execute("Response.write ""Hello"") .... would have the same effect as just using response.write "hello"

As soon as I saw this new feature I knew instantly what I could do with it:-

<% For Each Field in Request.Form TheString = Field & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>

We are setting variables automatically. That one little tiny piece of code, has saved me so much time over the last year, you will not believe.
Ok, it does have it flaws, Forms with image submit buttons pass form variable names like "submit.X", and "submit.Y" (Co-ordinates where you clicked on the submit image) which obviously wont do for variable names in ASP, and results in the white error page from hell.... But we can start to prevent these things:-

<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops TheString = tmpField & "= Request.Form(""" & Field & """)" Execute(TheString) Next %>

Why stop there?? There are many silly characters that are fine in HTML controls as names, but when setting variable names automatically like this in VBScript, these characters will not do. You can easily add additional lines as above to escape other characters like spaces, hyphens and all manner of other ones too.

You've got multiple controls on forms.... how are you gonna assign variables to multiple select boxes?
As a developer, I found it handy to read these 'multiple' part form variables into an array... I will build upon the above code to handle multiple items and create arrays from them:-

<% For Each Field in Request.Form tmpField = Replace(Field,".","") 'remove full stops ItemCount=Request.Form(Field).count IF ItemCount > 1 then
execute "redim " & field & "(" & itemcount -1 & ")" 'Dynamically dimension the array
For Item = 0 To ItemCount - 1
TheString = tmpField & "("&Item&")= request.form(""" & field & """).item(" & Item + 1& ")"
Next
Else
TheString = tmpField & "= Request.Form(""" & Field & """)"
End If
Execute(TheString)
Next
%>

I hope by now you can see the potential of what all of the above means to you as the developer.....
I urge you all to write your own functions that'll handle variables the way you want them to, and please remember.... FORMS were just an example, don't stop there, after all there are Querystrings, Cookies, Session variables, Application Variables, ServerVariables. Then there are recordsets (why not?) and the dictionary objects, etc, etc, etc.

Just remember, you are now armed with info which will save you a great deal of time, why not get busy writing some variable handling functions of your own??

If anybody wants a function to selectively handle Form, Querystring, Cookies, Session, Application and Server variables then I have one...just mail me. It also has a parameter to run in DEBUG mode which will show you variables in all objects, their values and whether their names present a problem during conversion. It'll also flag those controls that can be set to arrays, etc.

Anyway, those that do feel inspired to get writing their own handlers, let me ask one thing.... Send me a copy of your finished function(s), this is pretty new stuff and I'm well open to being inspired by YOUR code.

Feel free to contact me with your feedback.

Enjoy!
http://www.aspwebpro.com/tutorials/asp/newwaytohandlevar.asp

DISPLAY DATA FROM DATABASE

Alright, you have your database connection established and now you want to display data from your database on your page. Easy enough. We'll take the same code that we left off with from our Open Database Connection Tutorial except we will change the database table name to myCustomers and build from there.

First, create a database with a table called myCustomers with the following fields and add some data:
Company - text field
Address - text field
City - text field
State - text field
Zipcode - number field

As a reminder, here is our open database connection code:

<% DIM objConn Set objConn = Server.CreateObject("ADODB.Connection") objConn.ConnectionString = "DSN=myCONNECTION.dsn" objConn.Open DIM mySQL mySQL = "SELECT * FROM myCustomers" DIM objRS Set objRS = Server.CreateObject("ADODB.Recordset") objRS.Open mySQL, objConn %>

ASP Web Pro

Display data from database here.


Now, if you know that you only have one customer in your database to display, it is very simple. Just paste the below code into the body section of your page:



<% Response.Write objRS("Company") %>
<% Response.Write objRS("Address") %>
<% Response.Write objRS("City") %>
<% Response.Write objRS("State") %>
<% Response.Write objRS("Zipcode") %>



<% ' Don't forget to close your connection after you display your data. objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing %>

This will neatly display your customer's data on your page.
When displaying data from a database on your pages, it is a good idea to use tables to organize how the data will be displayed.

Now, ideally you will have more than one customer. If have multiple customers in your database, you need to do a little bit more



<% DO WHILE NOT objRS.EOF %> <% objRS.MoveNext Loop %>
<% Response.Write objRS("Company") %>
<% Response.Write objRS("Address") %>
<% Response.Write objRS("City") %>
<% Response.Write objRS("State") %>
<% Response.Write objRS("Zipcode") %>




<% ' Don't forget to close your connection after you display your data. objRS.Close Set objRS = Nothing objConn.Close Set objConn = Nothing %>

In the first example, our page displays the first and only record returned by our mySQL statement. In this example, the <% DO WHILE NOT objRS.EOF %> and <% objRS.MoveNext...Loop %> code instructs our page to display the first record returned by our mySQL statement, move to the next record, and loop back and continue the table and display the next record until there are no more records left.

So what happens if you have 100 customers and it is just too much information to display on one page? You would probably just want to display the first 10 or 20 records on your page and include a next and previous link at the bottom so that you can scroll through your records more easily. We will get into that later, but for now we will let you absorb this. Congrats!

SEARCH ENGINES

Search Engines are a vital tool on the Internet. They provide web surfers with a way to find what they are looking for. Some of the most popular Search Engines are Excite, GoTo, Lycos, and MSN. There are literally 1,000's more of them, but they all serve the same purpose for the most part.

In order to use a Search Engine, all you do is type in a single keyword or phrase, click search and voila, you have a list of related web sites. Depending on your keyword and which particular search engine you use, you could have anywhere from 10 to 10,000,000 web sites in your results. Ok, you probably already knew that much. The real question is how do I get my web site to show up in those results and show up within the top 10 results. Well, as in many things in life, it all depends.

The first thing you need is a good web site. The second thing you need is to have an effective set of keyword and description META tags in your home page. These META tags must appear in this format:




and they must be located within the tags of your home page. Your keywords can either be a single word or a phrase and must be separated with a comma like this "asp, active server pages". Your description is a brief and to the point description of your web site.
Many Search Engines have advanced tools that rank your web site according to keyword relevance. In order for your keywords to be considered relevant, they should be used in your description META tag and also within the text of your home page. As a general rule, your keywords should appear at least 3 times within the text of your home page in order for them to be considered relevant. Be careful, though. If your keywords appear more than 8 times on your page, it may be considered spam by search engines and be banned from their listing service all together.

Now, you have your web site and your META tags. What next? Well, now you need to submit your web site to the search engines. You can do this manually on your own each month, which could potentially take hours of your own time or you can use a low cost search engine submission service like our SubmitME! program.

When you submit your web site to the major search engines, you want to use keywords that are as specific as possible. The reason for this is that search engines are big business these days. They charge literally $1,000's of dollars per month to huge corporations that want their web sites listed at the top of a given category and if you have a small business or personal web site, you simply cannot compete with that. For example, let's say you have a car dealership web site and you use "cars, trucks, sports utility vehicle" as your keywords. When a web surfer searches for the term cars, they will get a long list of car manufacturers like Chrysler, Ford, and Toyota. They will also get a long list of car rental companies like Avis, Budget, and Hertz because those companies are paying the top dollar to be listed first. You probably would not find your listing until about 300 sites later if you are lucky. Once you get past the top paying companies, most search engines list smaller web sites in random order so there is really not much you can do about that. Sometimes you may be listed within the top 100 and sometimes it will be the top 100,000. Sure, there are a couple of tricks that you can try that may help a little bit, but unless you are ready and willing to pay for it, you are better off looking for other options.

So how do we solve this dilemma? Narrow down your keywords. Instead of just using keywords like these: "cars, trucks, sports utility vehicle" try using specific brands or names like these: "Ford Bronco, Toyota Corolla" or narrow it down geographically like this: "Charlotte NC car dealerships, NC Ford dealerships", etc.

What else can I do? Nowadays, there are lots of smaller industry specific directories and search engines you can use. For example, if you are a manufacturing company, you can list your web site with the Thomas Register Directory of Manufacturers. There are also lots of geographically oriented search engines like www.uk.goto.com for the United Kingdom.

Search engines are a necessity for the survival of the Internet and its web sites in general. If you do not list your web site with search engines, the only people can find your site is either by word of mouth or by accident. That's a tough way to grow your web site, so get your web site and META tags together and start submitting!

http://www.aspwebpro.com/tutorials/internet/searchengines.asp

SEARCH ENGINES

Search Engines are a vital tool on the Internet. They provide web surfers with a way to find what they are looking for. Some of the most popular Search Engines are Excite, GoTo, Lycos, and MSN. There are literally 1,000's more of them, but they all serve the same purpose for the most part.

In order to use a Search Engine, all you do is type in a single keyword or phrase, click search and voila, you have a list of related web sites. Depending on your keyword and which particular search engine you use, you could have anywhere from 10 to 10,000,000 web sites in your results. Ok, you probably already knew that much. The real question is how do I get my web site to show up in those results and show up within the top 10 results. Well, as in many things in life, it all depends.

The first thing you need is a good web site. The second thing you need is to have an effective set of keyword and description META tags in your home page. These META tags must appear in this format:




and they must be located within the tags of your home page. Your keywords can either be a single word or a phrase and must be separated with a comma like this "asp, active server pages". Your description is a brief and to the point description of your web site.
Many Search Engines have advanced tools that rank your web site according to keyword relevance. In order for your keywords to be considered relevant, they should be used in your description META tag and also within the text of your home page. As a general rule, your keywords should appear at least 3 times within the text of your home page in order for them to be considered relevant. Be careful, though. If your keywords appear more than 8 times on your page, it may be considered spam by search engines and be banned from their listing service all together.

Now, you have your web site and your META tags. What next? Well, now you need to submit your web site to the search engines. You can do this manually on your own each month, which could potentially take hours of your own time or you can use a low cost search engine submission service like our SubmitME! program.

When you submit your web site to the major search engines, you want to use keywords that are as specific as possible. The reason for this is that search engines are big business these days. They charge literally $1,000's of dollars per month to huge corporations that want their web sites listed at the top of a given category and if you have a small business or personal web site, you simply cannot compete with that. For example, let's say you have a car dealership web site and you use "cars, trucks, sports utility vehicle" as your keywords. When a web surfer searches for the term cars, they will get a long list of car manufacturers like Chrysler, Ford, and Toyota. They will also get a long list of car rental companies like Avis, Budget, and Hertz because those companies are paying the top dollar to be listed first. You probably would not find your listing until about 300 sites later if you are lucky. Once you get past the top paying companies, most search engines list smaller web sites in random order so there is really not much you can do about that. Sometimes you may be listed within the top 100 and sometimes it will be the top 100,000. Sure, there are a couple of tricks that you can try that may help a little bit, but unless you are ready and willing to pay for it, you are better off looking for other options.

So how do we solve this dilemma? Narrow down your keywords. Instead of just using keywords like these: "cars, trucks, sports utility vehicle" try using specific brands or names like these: "Ford Bronco, Toyota Corolla" or narrow it down geographically like this: "Charlotte NC car dealerships, NC Ford dealerships", etc.

What else can I do? Nowadays, there are lots of smaller industry specific directories and search engines you can use. For example, if you are a manufacturing company, you can list your web site with the Thomas Register Directory of Manufacturers. There are also lots of geographically oriented search engines like www.uk.goto.com for the United Kingdom.

Search engines are a necessity for the survival of the Internet and its web sites in general. If you do not list your web site with search engines, the only people can find your site is either by word of mouth or by accident. That's a tough way to grow your web site, so get your web site and META tags together and start submitting!

http://www.aspwebpro.com/tutorials/internet/searchengines.asp