Dynamic Preface: Many Web designers may have faced the problem of designing a form for users to input, the user entered the content into the database, but the user input is not the type of content (numeric field but input text) Situation or the user input content does not meet the requirements of the System Manager, but can not remind the user where the error in order to allow users to fill out the form, the average person may use the browser Back button to return to the form page and then fill in the wrong information, But if the program that joins the database is in the same program as the form's content, the user has no chance of returning to the previous page to refill the data. Most people usually use the session to solve this problem, but this brings a problem: the session occupies a large system resources! There are two ways to solve these problems: one is using the front-end control via JavaScript, the other is server-side control through ASP subroutines. Here is how to use the ASP subroutine for server-side control to complete. In the use of the database is also different from the past, I used this time to introduce the SQL 7.0, the following program is linked to the database is built in SQL 7.0 in the pubs database, users do not have to re-establish, as long as the stored procedures in the pubs database can be established.
Note: The following program I wrote for a supermarket design, because this is mainly about writing skills, so I do not do the rounding, but the reader can modify the contents as needed.
Bookstore.asp
<%@ transaction=required language= "VBScript"%>
<%Response.Buffer=True%>
<!--#include File=adovbs.inc-->
<%
Annotations: Establishing database Links:
SET Conn = Server.CreateObject ("ADODB. Connection ")
Note: Use the ConnectionString attribute of the database link to specify the database information you want to link, where we set the database driver to SQL Server, the database server name is jackal, and the database is the pubs database:
conn.connectionstring = "Driver={sql Server}" & _
"; Server=jackal; Uid=sa; pwd=;D Atabase=pubs "
Note: Open the database in the Open method:
Conn.Open
Annotations: Creating Command objects, and ActiveConnection properties to link database-linked objects Conn:
Set CMD = Server.CreateObject ("Adodb.command")
Cmd.activeconnection = Conn
Annotations: Setting up a data collection object; set rsform = Server.CreateObject ("ADODB. RecordSet "), defines strfeedback, strtitle string variables, representing form error messages and database error messages, respectively:
DIM Strfeedback, Strtitle
Annotations: Defining form variables:
DIM stor_id, Stor_name, Stor_address, city, state, zip
%>
<script runat= "Server" language= "VBScript" >
The next step is to write the Retrieveform subroutine, which is to receive the information provided by the form:
SUB Retrieveform (stor_id)
Note: with cmd.activeconnection = conn use, and the Commamndtype property is set to adCmdStoredProc, which means CommandText as a stored procedure:
Cmd.commandtype = adCmdStoredProc
Cmd.commandtext = "Proc_retrieve_store"
Note: Add a parameter "stor_id" to the Parameters.Append method as an input parameter for the stored procedure:
Cmd.Parameters.Append cmd. CreateParameter ("stor_id", Adchar, adparaminput,4,stor_id)
Note: Execute the stored procedure:
SET Rsform = Cmd.execute
Note: Delete stor_id parameters:
Cmd.Parameters.Delete (0)
End SUB
To complete the previous step, the next step is to write a Setfromform subroutine that writes the form values that were entered before the consumer back to the form:
SUB Setfromform
Note: Remove each field value from the form and pass it to the specified variable:
stor_id = Request.Form ("stor_id")
Stor_name = Request.Form ("Stor_name")
stor_address = Request.Form ("stor_address")
City = Request.Form ("City")
State = Request.Form (' state ')
Zip = Request.Form ("Zip")
End SUB
(Note: To remove a value from a data collection object and display it in the form, StrName represents the form name, intsize represents the form size, Intmax represents the maximum field length allowed for the form, Server.HTMLEncode (strvalue) represents the form content.) )
Sub Showtext (StrName, strvalue, IntSize, Intmax)
Response.Write "<input type=text name=" & StrName & _
"Id=" & StrName & _
"Onfocus=select ()" & _
"Size=" & IntSize & _
"Maxlength=" & Intmax & _
"Value=" & Chr & Server.HTMLEncode (strvalue) & Chr (+) & ">"
End Sub
The following is the EditForm subroutine, which is to update the data section:
Function EditForm ()
Annotations: Defining error message String variables:
DIM Strerr
Strerr = ""
IF not Len (Request ("stor_id")) > 0 THEN
Strerr = strerr & "Please enter number .<br>"
End IF
IF not Len (Request ("Stor_name")) > 0 THEN
Strerr = strerr & "Please enter name .<br>"
End IF
IF not Len (Request ("stor_address")) > 0 THEN
Strerr = strerr & "Please enter address .<br>"
End IF
IF not Len (Request ("City") > 0 THEN
Strerr = strerr & "Please enter city name .<br>"
End IF
IF not Len (Request ("state") > 0 THEN
Strerr = strerr & "Please enter province name .<br>"
End IF
Note: Returns the form error message back to EditForm:
EditForm = Strerr
End Function
The following is the Insertform subroutine:
Function Insertform ()
DIM strSQL
strSQL = "Proc_insert_store"
Note: Specifying commands to the database with the Command object's CommandText property is accomplished by proc_insert_store this stored procedure:
Cmd.commandtext = strSQL
Note: The CommandType property of the Command object determines that the form requested for the database is a stored procedure:
Cmd.commandtype = adCmdStoredProc
Note: Call the Setparms subroutine:
Setparms
Annotations: Execute a stored procedure with the command object's Execute method:
Cmd.execute
Note: Returns the MSG parameter attached to the Setparms subroutine to the strmsg error message string variable:
STRMSG = cmd. Parameters ("MSG")
Insertform = STRMSG
End Function
Next, establish the Setparms subroutine:
Sub setparms ()
Note: Add the Parameter object to the Parameters object collection as the command's Append method, as an input parameter for the stored procedure this is to obtain the content value of the form, which must be called only when updating or adding data:
Cmd. Parameters.Append cmd. CreateParameter ("Return_value", adinteger, adParamReturnValue)
Cmd. Parameters.Append cmd. CreateParameter ("id", Adchar, adparaminput,4,stor_id)
Cmd. Parameters.Append cmd. CreateParameter ("Stor_name", Advarchar,adparaminput,40,stor_name)
Cmd. Parameters.Append cmd. CreateParameter ("Stor_address", adVarChar, Adparaminput,40,stor_address)
Cmd. Parameters.Append cmd. CreateParameter ("City", adVarChar, Adparaminput,20,city)
Cmd. Parameters.Append cmd. CreateParameter ("state", Adchar, Adparaminput,2,state)
Cmd. Parameters.Append cmd. CreateParameter ("Zip", Adchar, Adparaminput,5,zip)
Cmd. Parameters.Append cmd. CreateParameter ("msg", adVarChar, Adparaminput,60, "")
End Sub
The following is the UpdateForm subroutine:
Function UpdateForm (IntID)
Note: Specifying commands to the database with the Command object's CommandText property is through PR