Populate Form from Database ASP Script - ASP Web Pro
  Active Server Pages Programming by ASP Web Pro

ASP Scripts

POPULATE FORM FROM DATABASE

Okay, so you want to make an editable form on your web page that retrieves data from your database and allows the user to update their own info. For our example, let's say you have a bunch of sales reps that work for your company. They login to your secure website area and want to update their personal info. For this example, we will say that a sales rep logged in to our secure area with their employee ID number, which is in turn written to a cookie on their browser.

First, we start off with our standard connection and recordset code:

<!--#INCLUDE VIRTUAL="/includes/connection.asp" -->

<%
DIM mySQL, objRS
mySQL = "SELECT * FROM tblSalesReps WHERE EmployeeID = " & intEmployeeID & " "
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.Open mySQL, objConn
%>

In this example, you could display the sales reps first and last name in two simple text boxes by doing this:

<form name="Customer" method="post" action="updatepage.asp">

First Name: <input type="text" name="FirstName" size="50" value='<%=objRS("FirstName")%>' ><br>
Last Name: <input type="text" name="LastName" size="50" value='<%=objRS("LastName")%>' >

<input type="submit" name="Submit" value="Submit">
</form>

If you wanted to display a sales territory field that retrieves and lists available terriroties from your database, you could display them with a drop down menu like this:

<form name="Customer" method="post" action="updatepage.asp">

Territory: <select name="Territory">
<option selected><%=objRS("Territory")%></option>
<%
DIM strTerritory
DO WHILE NOT objRS.EOF
strTerritory = objRS("Territory")
%>
<option value><%=strTerritory%></option>
<%
objRS.MoveNext
Loop

objRS.Close
Set objRS = Nothing
%>
</select>

<input type="submit" name="Submit" value="Submit">
</form>

This will display the sales reps saved territory and allow them to view all of the other territories in the drop down menu and choose another if they wish.

Last, you may have a comment field in your database for your sales reps to make additional comments. If you want them to be able to edit their comments, you could display them in a textarea box like this:

<form name="Customer" method="post" action="updatepage.asp">

Comments: <textarea name="Comments" cols="50" rows="5" wrap="VIRTUAL"><%=objRS("Comments")%></textarea>

<input type="submit" name="Submit" value="Submit">
</form>

That's it. Now, you have your very own editable form. If you need help with updating the record in your database, check out our Update Record script.

< Back to ASP Scripts


 

 

Main Menu
Home
ASP Scripts
ASP Tutorials
JavaScripts
Awards
Contact Us

Email This Page

Other Resources
ASP Web Hosting
Search Engine Submission
Programming Help

 

 

 

Other ASP Web Sites

Site Map

 

Last Updated:
04 February 2012