CGIDEV2 - Passing paramters

In the previous example, we have forgotten one common feature of web programming! Typically, we want to solicit information from our users. In the previous example we forgot to ask the users what year's data he wants to see on his report. In this revised program we get web parameter passed in, which we use a key in the setll and read.



Note in the example above, how we pass parameters by enumarating the parameter name and value (example: year=2013). The question mark (?) indicates the beginning of the first parameter. HTTP parameters are commonly passed using either GET or POST method. The above example illustates the GET method.

myCompanyUrl.com/MYCGILIB/SLSRPT2.pgm?year=2013

Read more on HTTP parameter passing methods and HTML forms


The code below is similar to the previous example, however in this example, we receive parameter year using zhbgetinput and zhbgetvar procedures. We use this paramter to serve as a key on our Sales file. Also in this example we are not shutting down program and releasing resources (*INLR = *ON). This provides a performance boost. As a result we have to explicitly open and close files or face inconvenience that files held open all day.

 *----------------------------------------------------------- *       
 * Program SLSRPT2                                            *       
 * Sample RPG program used to write an HTML stream file       *       
 * to web browser, uses CGIDEV2 service program.              *       
 * Compile with library CGIDEV2 in library list.              *       
 *----------------------------------------------------------- *       
                                                                      
 /copy qrpglesrc,hspecs                                               
 /copy qrpglesrc,hspecsbnd                                            
                                                                      
h dftactgrp(*NO) actgrp('SLSRPT2') DATEDIT(*YMD)                      
                                                                      
fSLFILE    IF   E           K DISK    usropn                          
 *                                                                    
 * Prototype definitions and standard system API error structure      
 /copy qrpglesrc,prototypeb                                           
 /copy qrpglesrc,usec                                                 
                                                                      
 * Indicators for GetHtmlIfsMult subprocedure                              
d IfsMultIndicators...                                                     
d                 ds                                                       
d  NoErrors                       n                                        
d  NameTooLong                    n                                        
d  NotAccessible                  n                                        
d  NoFilesUsable                  n                                        
d  DupSections                    n                                        
d  FileIsEmpty                    n                                        
                                                                           
 * Number of variables - use to store quantity of cgi variables passed in  
dnbrVars          s             10i 0                                      
                                                                           
 * Saved query string                                                      
dsavedquerystring...                                                       
d                 s          32767    varying                              
                                                                           
 * Create a WorkVariable data structure that can be cleaned up quickly     
d  WorkVariables  ds                                                       
d @Somevar1                     10i 0 INZ                                  
d @Somevar2                     10a   INZ(' ')                             
d @Somevar3                       n   INZ                                  
d @Year                          4a   INZ                                  
                                                                                     
 * Clear work my variables                                                           
c                   clear                   WorkVariables                            
c                                                                                    
 * Read externally defined template file. (We could actually use the getHtmlifs      
 * procedure here becuase we are only using one template file.)                      
                                                                                     
c                   eval      IfsMultIndicators = gethtmlifsmult(                    
c                             '/myIFSdir/salesReportIFS2.html':                            
c                             '<as400>')                                             

c                                                                                    
 * Get parameters passed from CGI program                                            
 * (this is not required, as we are not passing in any parameters in this example)   
c                   eval      nbrVars =                                              
c                             zhbgetinput(savedquerystring:qusec)                    
                                                                                     
 * clear any HTML output that has been buffered but not sent to the browser          
 * (not actually required here, but can be useful in some cases)                     
                                                                                     
c                   Callp     clrhtmlbuffer                                          
                                                                                     
  * get value for @Year passed in as CGI parameter                  
 c                   eval      @Year  = zhbgetvar('year')           
                                                                    
 c                   Callp     wrtsection('Top')                    
 c                   Callp     wrtsection('Header1')                
 c                   Callp     updHTMLvar('Year':@Year)             
 c                   Callp     wrtsection('DataTop')                
                                                                    
 c                   Open      SLFILE                               
                                                                    
 c     @Year         Setll     SLFILE                               
 c     @Year         Reade     SLFILE                               
 c                   Dow       not %EOF                             
 c                   Callp     updHTMLvar('Region' :REG07)          
 c                   Callp     updHTMLvar('Sales' :%char(SLS07))    
 c                   Callp     wrtsection('DataDetail')             
 c     @Year         Reade     SLFILE                               
 c                   Enddo                                          
                                                                    
 c                   Close     SLFILE                               
                                                                    
 c                   Callp     wrtsection('DataBottom')             
 c                   Callp     wrtsection('End')                    
                                                                    
  * *fini is special section sendsthe output buffer to the web client           
                                                                                
 c                   Callp     wrtSection('*fini')                              
                                                                                
 c**                 Eval      *INLR = *ON                                      
 c                   Return                                                     
 

The only change in the HTML template file is the addition of the substitution value called /%Year%/.

<AS400>Top                                           
Content-type: text/html                              
                                                     
   <HTML>                                            
   <BODY>                                            
<AS400>Header1                                       
   <H1>Sample Sales Report for /%Year%/ 
<AS400>DataTop                                       
   <table border =1 >                                
<AS400>DataDetail                                    
   <tr><td>                                          
   /%Region%/ </td><td > /%Sales%/                   
   </td></tr>                                        
<AS400>DataBottom                                    
   </table>                                          
<AS400>End                                           
   </BODY>