02
Sat, Nov
2 New Articles

Tech Tip: Give Me That ZIP Code, Please

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

This tip will show you how to simulate the F4 prompt key, which is built into every command in i5/OS (formerly OS/400). I have created an RPG CGI program to accomplish this task. But instead of just making a "normal" read-some-records-and-present-them-in-a-table program, I spiced it up with AJAX, a popular technique to make a part of a Web page interact with a Web server in a way you normally see only in PC programs.

I will once again use the HTML form from "TechTip: Validating HTML Forms with Message Subfiles." If you have not installed this, fear not because the prompting program can run as a standalone application as well.

The application I have created will ask you to begin entering the name of a U.S. city. As you type, the application builds a table that displays the results of what you might enter. You can then select a city and click on the ZIP code button, and the ZIP code will be parsed back to the HTML form. Figure 1 shows what it looks like.

http://www.mcpressonline.com/articles/images/2002/zipcodeV4--09010600.jpg

Figure 1: Start entering the letters of the name of a city, and the application will build a table for you. (Click images to enlarge.)

Note the optional ZIP code field. This is used if, for example, you enter Los Angeles and have selected to display only five rows. Then, you can move your cursor to the ZIP code field and enter the ZIP code.

The drop-down list makes it possible to control how many rows should be displayed in the table.

Have a look at the movie example in which I do a search for "Farmerville."

Because I have limited space here, I will not go into details about all the code in this tip, but I will point out some important parts.

You need to change two things in the FORM002H form in order to make it all work:

  • Create a JavaScript function that will create a pop-up window.
  • Create a new "Prompt" button on the HTML form.

Figures 2a and 2b show the lines to be added. (The code is at the end of this tip.)


http://www.mcpressonline.com/articles/images/2002/zipcodeV4--09010601.jpg

Figure 2a: Create a JavaScript function that will create a pop-up window.


http://www.mcpressonline.com/articles/images/2002/zipcodeV4--09010602.jpg

Figure 2b: Create a new "Prompt" button on the HTML form.

That was the simplest part. We will now move on to zipus.htm, which is the HTML document taking care of the cool AJAX stuff.

Figure 3 shows a subset of zipus.htm:

http://www.mcpressonline.com/articles/images/2002/zipcodeV4--09010603.jpg

Figure 3: This code is part of zipus.htm.

Notice the onKeyUp event at 1. This means that every time you press a key and release it, the getZip JavaScript function will be called, which will call the RPG CGI program. The

code at 2 is used within the getZip function, so I'll get back to that later.

Figure 4 shows a subset of the getZip function:

http://www.mcpressonline.com/articles/images/2002/zipcodeV4--09010604.jpg

Figure 4: This code is part of the getZip function.

The getZip function is found in the member getzipcodeus.js. It contains some housekeeping, which I will not get into, so just be happy that it works.

At A, the URL to the RPG CGI program must be entered (very important).

At B, the URL that was built at A and the next few lines are called.

At C, the result from the RPG CGI program will use the Data Object Model to parse back the data returned from the RPG CGI program, and the

tag in Figure 3 controls where the data will be placed on the Web page. Simple, isn't it?

If you're confused, read on: I will explain how to implement all this step by step. But first, a little about the RPG program.

Because zipus.htm passes a query string, we have to make a small change to CGIPARSEZ so that API QzhbCgiParse will accept this kind of parsing. Figure 5 shows the change. Look for ZIP1 and insert the same in your source. (Alternatively, you can download the member from the code at the end of this tip.)

http://www.mcpressonline.com/articles/images/2002/zipcodeV4--09010605.jpg

Figure 5: Make a small change to CGIPARSEZ.

The RPG CGI program FORM005 is a straightforward program, so I will not comment on that. Just download it and compile it according to the description in the header section.

OK, enough mumbo jumbo. Let's install.

  1. Create a directory in your Web server's document root called /ajax. If you do not know what the document root is, look in the Apache config file for a directive called DocumentRoot or for the directive.
  2. Download ajax.zip, unzip it, and upload everything to /documentroot/ajax on your Web server.
  3. Download FORM002H, CGIPARSEZ, and FORM005; unzip them, and upload them to QRPGSRC in your CGILIB. Compile CGIPARSEZ and FORM005 following the descriptions in the header sections. (Click to download.)
  4. I have also included the database files USZIPCODES and USSTATES used in FORM005. USZIPCODES comes in two versions: a full version that contains 42,741 records and a shorter version that contains 1,000 records. They are stored in a save file named ZIP, so select the one you prefer, unzip it, FTP it to your Web server, and then restore it to a library that is in the library list of your Web server job (very important if you want the program to work).

    Click to download the full version.
    Click to download the shorter version.
    Click to download the DDS descriptions of the files.


That's it! You're done! You're now ready to take it all for a spin.

If you have installed FORM002, enter the following URL:

http://your-server/cgi-bin/form002

If you have installed FORM05 from this tip, enter the following:

http://your-server/ajax/zipus.htm

You should now see a really neat way to look up ZIP codes.

One last thing to consider: When you use AJAX this way, you will get a lot of database access, which means that the RPG CGI program will be called many times, resulting in a lot of opening and closing of the databases. To speed this up, consider moving all the DB access out of the CGI program and into some kind of never-ending program. Then use for example a data queue to communicate between the CGI program and the DB access program.

In my next tip, I'll add a cool HTML/JavaScript feature, so stay tuned.

Jan Jorgensen is a programmer at Electrolux Laundry Systems Denmark. He works with stuff like RPG, HTML, JavaScript, and Perl. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it..

Jan Jorgensen

Jan Jorgensen is one of the owners of www.reeft.dk, which specializes in mobile and i5 solutions. He works with RPG, HTML, JavaScript, Perl, and PHP. You can reach him at This email address is being protected from spambots. You need JavaScript enabled to view it.

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: