Tutorials

Back        

Form to database Download the code
 
The above code creates a simple form asking for name, email and comments. The form knows which page to post the details too i.e. action="add_to_database.asp". Now onto the ASP action page, i.e. the page that will receive the details sent from the form and process them. Create a new ASP page called 'add_to_database.asp'. Keep it in the same folder as 'form.html'. Copy and paste the following ASP code into 'add_to_database.asp'. <%@ Language="VBScript" %> <% Option Explicit %> Form to database <% 'declare your variables Dim name, email, comments Dim sConnString, connection, sSQL ' Receiving values from Form, assign the values entered to variables name = Request.Form("name") email = Request.Form("email") comments =Request.Form("comments") 'declare SQL statement that will query the database sSQL = "INSERT into users_tbl (name, email, comments) values ('" & _ name & "', '" & email & "', '" & comments & "')" 'define the connection string, specify database 'driver and the location of database sConnString="PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=" & Server.MapPath("Users.mdb") 'create an ADO connection object Set connection = Server.CreateObject("ADODB.Connection") 'Open the connection to the database connection.Open(sConnString) 'execute the SQL connection.execute(sSQL) response.write "The form information was inserted successfully." ' Done. Close the connection object connection.Close Set connection = Nothing %>