Tuesday, April 18, 2017

Dynamic HTML with CGI Programming

Hello Friends,

Last couple of articles, we have been working with some static HTML. Now let’s go a step ahead and create a dynamic HTML with CGI. We are also going to see how to deal with database (PF) as well.

Typically we are going to do below stuffs,
  1. Create a RPGLE program which can get input from browser
  2. Check if we have any data in our database for the input
  3. Respond to browser with the data retrieved from PF
Basic Idea:

I am going to have a database called ORDERS. This will be having order information. We shall get order number from the browser input and will return the order information.




















Basically I am going to return the values in the form of HTML Table. So we need to construct table header (<TH>) and table data (<TD>) based on the number of rows.

In summary http://youras400ip/cgi/ordlist/123 should return details of order 123 in table format.

Here ORDLIST is RPGLE , 123 is order number

How to parse URI:

In order to get the order number from the URL we have to parse it. Using IBM procedure GETENV we can retrieve environment variable. Below command will help to retrieve the full URI. 

 Uri = %Str( GetEnv('REQUEST_URI') );   In our case URI = “/cgi/ordlist/123”

Once you get this it is easy to find order number from this. 

 ID1 = '/ordlist/' ;
 Pos = %Scan(ID1: Uri) + %Len(ID1) ;
 InpOrd = %int(%Subst(Uri:Pos));    

Once you get the order number then it is generic programming logic to loop through the order number in ORDERS file and for each record build the Table Detail HTML tag and write it into browser using QtmhWrStout.

Setll  InpOrd  Orders;                                
ReadE  InpOrd  Orders;                                
DoW Not %Eof(Orders);
   Data = '<tr>'                                   +              
            '<td>'+ %Trim(%Char(OrdNo))   +'<td>'  +  
            '<td>'+ %Trim(OrdItm)         +'<td>'  +        
            '<td>'+ %Trim(%Char(OrdQty))  +'<td>'  + 
            '<td>'+ %Trim(%Char(OrdDate)) +'<td>'  +
          '<tr>'                                   ;              
QtmhWrStout(Data: %len(%trimr(Data)): Err);           
ReadE  InpOrd Orders;                                 
EndDo;                                                

How to set library list:

We may have our PF in some other library or we may have different PF sitting in different library. In this case we have to instruct system to load require library list for running the program. This can be done by adding QIBM_CGI_LIBRARY_LIST command in httpd.conf

Multiple libraries can be given by separating with ";"

SetEnv QIBM_CGI_LIBRARY_LIST "LIB1;LIB2;LIB3"








This is it. Just restart your Apache server and point to your browser for accessing the program.

Full version of program is available here .

http://youras400ip/cgi/ordlist/123












http://youras400ip/cgi/ordlist/555












http://youras400ip/cgi/ordlist/1234













So how do you feel..!!! Interesting right... Again it is just simple prototype. We can do even more using CGI. I will try to cover another topic in my next post. Until then...

Have Fun...!!! Happy Coding...!!!

No comments:

Post a Comment