Generating an AJAX Response from the IBM with RPG and PHP
Alex Roytman: Hi, I’m Alex Roytman and with me here today is Hany Elemary. a web developer and analyst here at Profound Logic. Hany welcome and thanks for joining me.
Hany Elemary: I’m glad to be here Alex. Thanks for having me.
Alex Roytman: Well, thanks for making and commute all the way down the hall way.
Hany Elemary: Oh sure.
Alex Roytman: Well as a recap for our audience in my past videos I've introduced you to the importance of AJAX and I briefly discussed how we make an AJAX request from a web page. Now, what I want to do today is explain how we generate a response from the server to an AJAX request, so are you ready to get started?
Hany Elemary: That sounds good to me.
Alex Roytman: Hany I know that you were glad with AJAX. How difficult was it for you to learn how to write an AJAX response for them?
Hany Elemary: Well, Alex at first to be honest it was a little bit complicated before I knew anything about the subject but once I’ve got into it I actually discovered that it’s quite simple. It’s probably the easiest thing that you can do in web development.
So, for example, one of the most basic tasks in web development is writing a dynamic output page that has some HTML in it. Well, an AJAX response is really just a simple five version of that, so in a way like it’s dynamic response but you’re not outputting the entire HTML. However, you’re just outputting a subset or you’re outputting the information that the AJAX requests as it is expecting and a lot of times it just plain text.
Alex Roytman: That’s actually a very good way of putting it and just to clarify for our audience. So what you’re saying is that building an AJAX program is just like building a dynamic HTML page except your output is a bit of text instead of outputting the HTML.
Hany Elemary: You’re absolutely right but in this case you’re not just limited to plain text. You can actually output any data interchange format such as XML or JavaScript object notation or the reason as you know it, so it’s really flexible in that sense.
Alex Roytman: I understand that today you’re going to demonstrate for us how to output a product description in plain text in response to an AJAX request that’s providing a product ID.
Hany Elemary: Absolutely and that will be great.
Alex Roytman: Okay, now I understand that you’ll be using the IBMI as your server. What language on the server will you be using?
Hany Elemary: Well, let’s see what are our options here or we can do RPG and we can also do a PHP example so we can do either or we can do both.
Alex Roytman: I think I prefer to do both but how about we do it this way. You do an example using PHP and what I’ll do is I’ll do an example for our viewers in RPG.
Hany Elemary: I think that sounds like a good idea let’s see what we can do here today.
Alex Roytman: I think that is something our audience will really enjoy.
Hany Elemary: I think so too. Okay, let’s watch through the PHP code one line at a time. First, we get the information that was passed from the webpage. In our case that’s the product ID then we connect to the DB2 the database using the proper credentials. In this case I’m assuming that the database, user and password variable to hold my database credentials.
Then we need to execute the sequel statement so I’m selecting the product description and notice here that there’s a question mark in my where class. This basically expects an input parameter.
The next step is to bind the product ID parameter to our sequel statement instead of that question mark. Next, we execute the statement, position the cursor on the record, grab the filled contents from the database and print the description as our AJAX response, so you see the script was quite simple. That was about 10 lines of coded cell.
Now, let’s take a look at the application. Here we’re passing the product ID as an AJAX request and the moment we tab off the field we get to description as the AJAX response right next to the text field and that was really about it.
Now back to you Alex.
Alex Roytman: Alright, now let’s look at the exact same code using RPG-CGI. Something you’ll notice straight away about AJAX programs is that it can be very short and simple. In AJAX program typically it handles a very specific task like in this case providing a product description based on the product ID.
The code that you see here is a fully functional AJAX response program and it’s literally two lines of actual code. To get started you’ll need some kind of web framework or service program for RPG-CGI. Something like CGI DB2 for example or in my case I’m using the RPGsp environment.
Notice that I haven’t included the slash free and in free lines for free format because they are automatically implied in this environment, so first I have declared my product’s file using an aspect. The name of the file is PRODP I will prompt for you here so you can just see the details. It’s a standard input file.
Next, on my two lines of code here we read an input parameter from the AJAX request either post or get and assign it to an RPG variable. In RPGsp the RPGspInVal function accomplishes that for us when dealing with numeric values. Other frameworks will have similar functions to accomplish the same.
Then we grab the appropriate record from the database. I could have used them better to ask you all here, but this particular task is probably easier done with RPG’s record level access, so I’m just using a basic chain operation.
In this case if the record is found the PDESC field will automatically be populated with the product description and finally I send the results in plain text format. The bracket symbols are just a quick way to send any variable or expression to the browser and that’s all there’s to it.
Hany, thank you again for joining me today.
Hany Elemary: No, Alex thanks for having me. It was my pleasure.
Alex Roytman: And for our viewers if you have any questions for either myself or for him feel free to post them on www.ProfoundLogic.tv. Be sure to watch out for follow-up videos on AJAX and other topics as they relate to web development and modernization from the IBMI. Thank you and I’ll see you again soon.
Transcription by:
Scribe4you Transcription Services