Ju-chan
Ju-chan

Reputation: 508

How to Process Forms in a separate ASP file

I have a question.

I am learning classic ASP today for my next project. I am currently a .NET developer and using ASP.NET on that project is not a requirement of my client.

I have below a login page script.

Default.asp

<form method="post" action="ASP/aspLogin.asp">

  form code here...

  <input type="submit" class="Button Is_Default" value="Login"></input>

</form>

Now, what I have in my test ASP/aspLogin.asp page is as follows:

ASP/aspLogin.asp

<%@ Language="VBScript" %>

<%
    Dim strUsername
    Dim strPassword    

    strUsername = Request.Form("txtUsername")
    strPassword = Request.Form("txtPassword")

    If strUsername <> "" And strPassword <> "" Then
        Response.Redirect("Index.asp")
    End If

%>

When I ran the scripts above, the browser just redirected me to ASP/aspLogin.asp. I would want to redirect the user to his respective home page.

My objective is that I want my ASP/aspLogin.asp file do the processing of my form instead of placing the process above the Default.asp page. May I know if I missed something out here or there are some more things to consider to create the code I need. Resources would be also appreciated.

Upvotes: 2

Views: 1044

Answers (1)

John
John

Reputation: 4638

If you have users logging in and out then I'd use session variables (which I'm sure you've encountered in .net), so something along the lines of.

If strUsername <> "" And strPassword <> "" Then
    Session("Username") = strUsername
    Response.Redirect("Index.asp")
End If

You would then be able to add logic to index.asp and any other page to display data depending on the value of Session("Username").

This code demonstrates the concept. Obviously you would need to ensure that usernames aren't duplicated. In practice I'd recommend a database query to retrieve the primary key value of the users record which corresponds to both username and password

Edit.

So you're basically trying to emulate .net with Classic ASP. Two things to remember. Classic doesn't have code behinds, and a .net webform can only be posted to itself, (which gets really frustrating for someone moving from Classic to .net)

Probably you're best option is to have your form page post to itself and put your processing code at the top of Default.asp, with logic which only triggers it if there's a form submission, ie

<%@ Language="VBScript" %>

<%
    Dim strUsername
    Dim strPassword    

    strUsername = Request.Form("txtUsername")
    strPassword = Request.Form("txtPassword")

    If strUsername <> "" And strPassword <> "" Then
        'Your processing code
        Response.Redirect("Index.asp")
    End If

%>

<html>
<body>

<form method="post">

  form code here...

  <input type="submit" class="Button Is_Default" value="Login"></input>

</form>
</body></html>

An extension of this method would be to use asplogin.asp as an include. (You would need to remove the <%@ Language="VBScript" %> from the top of asplogin.asp, as it would become the middle of a page and you would already have your declaration in default.asp). Default.asp would then look like this.

<%@ Language="VBScript" %>

<!--#include file="ASP/aspLogin.asp"-->

<html>
<body>

<form method="post">

  form code here...

  <input type="submit" class="Button Is_Default" value="Login"></input>

</form>
</body></html>

This is about as close as you can get to having a codebehind in Classic

Upvotes: 3

Related Questions