Creating a PDF that submits data to a SQL Server Database Using .ASP

In this article we’re going to show you how to create a PDF form that will submit the users responses to a SQL Server database. To do this we’re going to take a standard PDF Form and place some form controls on it as well as a submit button.

 

For this tutorial you will need to have installed Microsoft IIS and SQL Server Express or Higher on a computer on your network or web site and be familiar with creating databases, tables and setting permissions for IIS and the database.

 

A Classic Active Server Page will be used for the tutorial, but you can easily translate it to the language of your choice.

Step 1 - Turn On The “Form” Menu Option

The first thing you should do, if you haven’t already done so is to turn on the “Form” Menu Option.

 

To do this, right-click on the menu area near the top of PagePlus and ensure the “Form” menu option is checked.

Click HERE to download the PagePlus X4 Project File

IMPORTANT NOTE ABOUT THE PROJECT FILE

 

You should delete the submit button (since it points to our Collect.asp script) and then add a new Submit button as shown in Step 7 below to cause the form to be submitted to your server’s collect.asp page.

Step 2 - Create a table in your SQL Instance

This article assumes that you are familiar with SQL Server or the database of your choice.

It is suggested that you follow this article in order to learn the techniques that are demonstrated. After that, the moon is the limit of what you can / want to do.
Therefore, please follow the naming conventions and so forth set out in the example - otherwise you may experience unnecessary difficulty since the code is set up to work with the naming conventions outlined here.
First create a database or use an existing one.

Next, create a table in your SQL Instance called: PDFResults

For the sake of simplicity, we’re going to only create three (3) fields and we’ll make all of the VarChar(50).
Populate the table with the following fields.


Id, int - Primary Key; Not Null, Identity Specification –> (Is Identity = Yes)

value_1, VarChar(50)

value_2, VarChar(50)

value_3, VarChar(50)

Set permissions for the table to allow internet IWAM access to the table.

When you are finished, your table should look like the diagram below in the SQL Server Table Designer.

Later in this article, we’ll provide you with the .ASP scripts that will work with the PDF Form and the Database.

Step 3 - Open PagePlus X4, Create PDF

For this tutorial we selected a stock .PDF form that came with PagePlus X4.

Create the stock newsletter (or blank page) from within PagePlus X4 and add the following form controls from the “Form Menu”.
·
One Text Field
·
One Check Box
·
Three Radio Buttons

Do not place the submit button of the form at this stage.
Your form should look similar to this (except without the submit button).

Step 4 - Naming the Text and Checkbox fields

Right-Click on the Text Field and select the “Form Field Properties” menu option.

When it appears, ensure you are on the “General” tab.

In the Name Field, type in “value_1” as shown below.

The name you type in here will be returned when the Submit button is clicked.

When you create your own unique fields in your own projects, you’ll want to replace this name with a name more fitting to the field it’s intended for.

For example, if you have a field in your table called CouponCode you might type “Coupon_Code” into the name field.

But we’re doing a tutorial, so stick with the program and simply type in “value_1” as shown below.

Then click OK.
Right-Click on the Check Box Field and select the “Form Field Properties” menu option.

When it appears, ensure you are on the “General” tab.

In the Name Field, type in “value_2” as shown below.

Then click OK.

Step 5 - Naming the Radio Button fields

Right-Click on the first radio button field and select the “Form Field Properties” menu option.

When it appears, ensure you are on the “General” tab.

The radio button fields are a little bit different if grouped together as they are in this example.

When we named the fields in the text field and check box field, the “Name” represents the value that is returned to the collect.asp script that we’ll create later in this article.

But when you create a ‘group’ of radio buttons, this is not the case.

Instead, the “Field Group” value is what is returned to the collect.asp page and the “Name” value is the value returned for the “Field Group”.

Type ‘Yes’ into the “Name Field” and let’s call the “Field Group” - ‘YesNoMaybe’.

Then click OK.
Right-Click on the second radio button field and select the “Form Field Properties” menu option.

When it appears, ensure you are on the “General” tab.

Type ‘No’ into the “Name Field” and let’s call the “Field Group” - ‘YesNoMaybe’.

Then click OK.
Right-Click on the Third radio button field and select the “Form Field Properties” menu option.

When it appears, ensure you are on the “General” tab.

Type ‘Maybe’ into the “Name Field” and let’s call the “Field Group” - ‘YesNoMaybe’.

Then click OK.

Step 6 - Creating ‘Collect.asp’

Before adding the submit button, we need to know where we’re going to ‘point’ the submit button to.

Open notepad or your favorite script editor and paste in the following Active Server Page script.

Save this file as “Collect.asp” to a web site location on your web server.
<%
Dim sValue_1
Dim sValue_2
Dim sValue_3
'The contents from the .PDF file are interpreted using Request.Form
Response.write("Form Contents: " & Request.Form) & "<BR>"
sValue_1 = Request.Form("value_1")
sValue_2 = Request.Form("value_2")
sValue_3 = Request.Form("YesNoMaybe")
'if a checkbox is left un-checked the value is empty/null
'set a checkbox value to 'No' if the value is empty/null
If Len(Trim(sValue_2)) = 0 then sValue_2 = "No"
Response.write("value_1: " & sValue_1 & "<BR>")
Response.write("value_2: " & sValue_2 & "<BR>")
Response.write("value_3 (YesNoMaybe): " & sValue_3 & "<BR>")
'DATABASE CODE
Dim SQL
Dim strConnection
Dim commInsert
SQL = "Insert INTO PDFResults (Value_1, Value_2, Value_3) VALUES ('" & sValue_1 & "','" & sValue_2 & "','" & sValue_3 & "')"
Response.Write("SQL: " & SQL) & "<BR>"
strConnection = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=LOGIN;password=PASSWORD;Initial Catalog=Internet;Data Source=SERVERNAME"
‘or
'strConnection = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Internet;Data Source=SERVERNAME"
Set commInsert = Server.CreateObject("ADODB.Connection")
commInsert.Open strConnection
commInsert.Execute(SQL)
commInsert.Close()
Set commInsert = Nothing
%>
Notice that I’ve retained the naming conventions that we used in the PDF Form and also notice that “sValue_3” is different (because it’s a radio group) in that it’s asking for Request.Form("YesNoMaybe") which is the “Field Group” for the three (3) radio buttons.

You will need to replace the “User ID”, “Password” and “Data Source” in the connection string to conform the the connection string required to access your SQL Server instance.

We’re almost ready to add the submit button now.

Have regard to where you saved “Collect.asp” on your web server.

For example, let’s say we have a web site called “MyWeb” and we created a folder called “PDFSubmissions” under the “MyWeb” folder.

Therefore to access the “Collect.asp” page we’d type: http://www.MyWeb.com/PDFSubmissions/Collect.asp
Determine what the correct URL path would be based upon your web site name and the folder name you saved “Collect.asp” to.

Step 7 - Adding the Submit Button

First, if you’re using the example project file, DELETE the Submit button and follow the steps shown below.

Now that we’ve created the form fields on the pdf and within the database table and know where we saved the collect.asp page to - we’re ready to add the submit button. From the “Form Menu” select the “Submit Button” option and click on the PDF form to add the button.
When you click on the form, a Submit Button Wizard will appear. Follow the screen shots below.

When you get to the wizard page that says “Please enter your server address so that your data can be sent to you” - type in the URL associated with your web site and the location of “Collect.asp”

E.G., http://www.MyWeb.com/PDFSubmissions/Collect.asp

Step 8 - Testing Things Out

Now you’re ready to publish the PDF file and test out the submit button and the “Collect.asp” script.

Go to “File - Publish as PDF...” menu option and publish the PagePlus X4 page as a PDF. When the PDF form has been created, it will open automatically.

Type some text into the Text Field. Place a tick into the Check Box field and select a radio button.

Click the Submit button.

Summary

The most likely issue you will have is getting the “Collect.asp” script working is to
·
A) Get the correct connection string settings and
·
B) Having the correct permissions on the Table.

Now that you can see how easy this is to do, you can begin to create your own ‘real-world’ PDF forms with database capability.
Home

Commercial & Custom Web & Desktop Products

DataObjx is a participant in the Microsoft® Independent Software Vendor Program

DataObjx.net

Home Products Desktop Web Sites Technologies Our Services Tutorials About Us Contact Us