In my previous article ("Getting Started with CGIDEV2 – Part 1"), I discussed how to write a program using CGIDEV2 to produce a simple report for the Web. Now it's time to look at how you can get information from a Web page in a browser into your RPG program.
The CGI APIs used to retrieve and process data from the Web are cumbersome, to say the least, so this is one of the areas where CGIDEV2 proves invaluable.
Since it is rare that you will fit a complete report on one Web page, let's look at another version of the Product Report used in the previous article, which allows the user to "page" through the report.
Figure 1 shows an example of a page of the report. The list is by Product Category, and the user can click on the "Next Page" link to view the products in the next category.
[TABLE]Figure 1: Output from a simple "print" style CGI program with Next Page option
The HTML
So how does a browser send information to a program? There are two methods that can be used – GET and POST. We will look at the difference between the two in a moment but the end result of both is that the browser sends a parameter string along with the program call. You have often seen these in URLs when accessing the internet. A URL with a parameter string has the following format:
//server/directory/programname?parm1=value&parm2=value&parm3=....
The parameter string starts with a '?' followed by the name of a parameter field, followed by the value of the parameter. Subsequent parameters are delimited by a '&'.
The paging logic is that when the user clicks the next page link, the program is called along with a parameter identifying the category to be listed.
Figure 2 shows the HTML for the report page. In a change from the example in the previous article, this html source is using delimiters of '' to indicate a section and '' to indicate a variable
To continue reading for free, register below or login
To read more you must become a member of Search400.com
');
// -->

; this makes the html document a little easier to manage with a design tool like WebSphere Developer's Studio Client (WDSC).
Figure 2: HTML for report page with Next Page link.
The important section is nextlink at the very end of the source. It identifies the Next Page text with a hyperlink to recall the RPG program and pass a parameter which identifies the next category code to be displayed. For example, when displaying the list of products for category '01' and where '05' is the next category, the Next Page link would have a hyperlink of
cgi-bin/cvallrep2a?CGICatCod=05
The CGI program
Figure 3 shows the source of the CGIDEV2 program CVALLREP2A. What you are really interested in here is how the program receives the parameters passed from the browser because they are not passed using a good old parameter list. The program makes use of five of the CGIDEV2 subprocedures: gethtmlIFS, wrtsection, updHTMLvar, ZhbGetInput and ZhbGetVar.
Figure 3: A simple CGIDEV2 program with Next Page logic.
Important items to note:
Running the program
With the server running, open your browser and enter this URL:
http://iseriesservername/cgi-bin/CVALLREP2A
where iseriesservername is the name of your iSeries. All being well, the results of your efforts will be displayed in the browser. Clicking on the Next Page link should bring you to the next category, until you reach end of file.
Using a form
The simple way of passing parameters to a program is to pass them on the hyperlink. But what if you want to have the user enter a value? Figure 4 shows an example of a form being used to request a starting category. The user enters a category code and clicks on the Submit Query button to call the CVALLREP2A program with the entered category.
[TABLE]Figure 4: Using a form to request a value.
The form is stored as a static HTML document. (You do not need to use a CGI program to display it.) Figure 5 shows the HTML for the form.
Figure 5: HTML for a form.
The definition of the form is delimited by a <FORM> and a </FORM> tag. The <FORM> tag contains the name of the form (getCategory), the action to be taken when a submit button is pressed (identify the program to be called) and the method to be used (GET or POST).
A GET method means that the parameter string will be passed exactly as it is on the Next Page hyperlink, and the parameter string will appear on the URL when the program is called. A POST method means that the parameter string does not appear on the URL when the program is called.
If you were using the standard CGI APIs you would have to use different APIs to retrieve the parameter string after first determining whether GET or POST was used when the program was called. One of the features of the ZhbGetInput subprocedure is that it does all that for you and you don't really care whether GET or POST was used. Having said that, POST means you get an easier to read URL.
Within the form you can have standard text along with the definition of input fields. Input fields are identified by an <INPUT> tag. The <INPUT> tag contains the type of entry (text and submit in this example -- more about those in a moment), the name of the parameter field (CGICatCod to correspond to what the program is expecting), the Maximum Length of the field and the value to be displayed.
A type of text means the input field is simply text. HTML does not cater for numbers, so even if you are entering a number, you define the type as text. A type of submit means it is a submit button and clicking it causes the form to be submitted (i.e. the program identified in the action for the form is called). You can change the text displayed in the button by giving it a value. In the next article we will look at the various types available (there are lots of them) and how to start customizing the use of submit buttons.
And what changes need to be made to the CVALLREP2A program? That's easy -- NONE!
Changing the Next Page link
One of the downsides of hyperlinks is that they are implicitly GET methods; so why not change the Next Page link to use a button with a POST method instead of a hyperlink? In the CVALLREP2A HTML document simply change the definition of the nextlink section to that shown in Figure 6. The type of hidden is the same as a hidden field in a display file. Also note the value for the submit button.
Figure 6: Using a form to define a button as opposed to a hyperlink.
One step further
Now you know how to send data from a program to a browser and how to get data from a browser into a program. In the next article you will see how easy it is to use those capabilities to write a maintenance program.
---------------------------
About the author: Paul Tuohy is CEO of ComCon, an iSeries consulting company. He is the author of Re-Engineering RPG Legacy Applications and is one of the quoted industry experts in the IBM Redbook "Who Knew You Could Do That With RPG IV?"