The Insert Script, Part 3: Redirection
Suppose a user fills out an HTML form on your site and then clicks the submit button. Your insert script opens your database, stores that information in the database, and then closes the database. What happens next? Well, typically either a "Thank You" message pops up on the user's browser or some kind of summary page appears, especially if the form was some kind of survey. How does the "Thank You" message or summary page appear? The answer is that your insert script tells the user's browser to load a new web page. The command to tell the user's browser to load a new web page is shown in Listing 23.
Listing 23 General Code to Redirect the User's Browser to a New Web Page
response.redirect("web page address")
Suppose that after the user fills out our guest book we want him or her to visit the InformIT web site. The last line in the insert script would be the following:
response.redirect("http://www.InformIT.com")
If we want the user to go to a thank-you web page we designed whose name is thankyou.html, the last line in the insert script would read like this:
response.redirect("thankyou.html")
The above line assumes that thankyou.html is in the same folder as the insert script; that is, thankyou.html is in the same folder as insert.asp on the web server.
In our guest book example, we'll redirect the user to a web page that displays the entire contents of the guest book. An ASP script named retrieve.asp creates this web page. Thus, this is the last line in our insert script:
response.redirect("retrieve.asp")
The completed insert script should looks like Listing 24.
Listing 24 insert.asp: Final Code with Browser Redirect Command
<% ' ' Step 1: Open the Guest Book ' set conn=server.createobject("adodb.connection") conn.open("DBQ=" & server.mappath("visualtutorial.mdb") & _ ";Driver={Microsoft Access Driver (*.mdb)};") ' ' Step 2: Read the user's information ' v_name=trim(request.form("name")) if v_name="" then v_name="anonymous coward" v_name=replace(v_name, "'", "''") v_email=trim(request.form("email")) if v_email="" then v_email="Not specified" v_email=replace(v_email, "'", "''") v_age=trim(request.form("age")) if isnumeric(v_age)=false then v_age=2 v_gender=trim(request.form("gender")) v_comment=trim(request.form("comment")) if v_comment="" then v_comment="No comment given" v_comment=replace(v_comment, "'", "''") v_hideEmail=trim(request.form("hideEmail")) if v_hideEmail="" then v_hideEmail="No" ' ' Step 3: Send a SQL INSERT query ' conn.execute("insert into GuestBook " &_ "(name,email,hideEmail, age,gender,comment) " & _ "Values (" & _ "'" & v_name & "'," & _ "'" & v_email & "'," & _ " " & v_hideEmail & " ," & _ " " & v_age & " ," & _ "'" & v_gender & "'," & _ "'" & v_comment & "')") ' ' Step 4: Close the database ' conn.close set conn=nothing response.redirect("retrieve.asp") %>
You're halfway there! Next, we look at how to create the retrieve script: retrieve.asp.