Tips_tutorials   >   Studiojs201   >   Search Countries Web Page

Search Countries Web Page

So far, we have been sending our HTTP requests to the web server by manually typing the URL in the web browser. Obviously that isn't a very user friendly way to send requests to the Omnis Web App Server.

We are ready to create a static web page that uses JavaScript to send a request to the Omnis Web App Server when the user clicks the Search button.

This tutorial is not intended to teach you everything you need to know about HTML and JavaScript. If you decide to develop an Omnis Studio ultra-thin web application you will need to purchase a couple of books on JavaScript, HTML, and CSS. Two books which I recommend are:

  1. JavaScript Bible by Goodman, Morrison
  2. HTML, XHTML, and CSS Bible by Pfaffenberger, Schafer, White, Karow.

You can buy these books online from Amazon.

You will need to use a text editor to create and edit your web pages, JavaScripts, and CSS.

Tip

If you are on the MacOS X platform get BBEdit if you don't already have it. It is the programmer's best friend for editing HTML, JavaScript, CSS, PHP, etc.

Create Search Page

  1. Using a text editor create an HTML file with the following content. (You can copy and paste the HTML from here.)

    <html>
    <head>
    <title>Countries</title>
    </head>
    <body>
    <h3>Countries</h3>

    <p>Enter the first letter of a country and click the Search button</p>

    <form name="SearchForm" method="POST" action="http://localhost/cgi-bin/nph-omniscgi"> <!-- start SearchForm -->

    <input type="text" name="SearchValue" size="1"/>

    <input type="submit" value="Search" title="Click to search for the records." />

    <input type="hidden" name="OmnisServer" value="5912" />
    <input type="hidden" name="OmnisLibrary" value="ContactsWeb" />
    <input type="hidden" name="OmnisClass" value="rtCountryList" />

    </form> <!-- end SearchForm -->

    </body>
    </html>

  2. Replace cgi-bin/nph-omniscgi with the appropriate cgi or script as applicable to your local host.
  3. Save the text file as searchcountries.htm to your local host web server directory.
  4. Open the FireFox web browser.
  5. Load the web page. http://localhost/searchcountries.htm
  6. Click the Search button. All going well the list of all the countries in the database will be returned to you.
The above HTML is the simplest form we can use for sending a message to the Omnis Web App Server. Later in this tutorial we will add some JavaScript.

Add $getWhere to tBase

We need to modify our Omnis Studio application to handle searching for records which begin with a certain value. The LIKE operator in SQL is used to search for records which begin with, contain, or end with, a certain value.

We need to add a $getSelect method to tBase to make it easy for use to pass in a WHERE clause and get back the records from the database.

  1. Add the method, $getSelect, to tBase.
  2. Add a Character type parameter pWhereText to the method.
  3. Copy the code from $getAllRecords to the $getSelect method.
  4. Modify the $getSelect method code so that it reads as follows:

; Prepare the ORDER BY text.
If len(pOrderBySQL)
   Calculate OrderBy as pOrderBySQL
Else
   Calculate OrderBy as $cinst.$:DefaultOrderBy
End If

Calculate SQLText as pWhereText
If pos("WHERE ",$cinst.$extraquerytext)
   Calculate SQLText as replace(SQLText,"WHERE ","AND ")
End If

; Prepare the SQL text to exclude the empty zero(0) primary key record.
Calculate ColName as $cinst.$:PrimaryKeyColName
If len(ColName)=0
   Calculate FlagOK as kFalse
Else
   
   If pos("WHERE ",$cinst.$extraquerytext)|len(SQLText)
      Calculate SQLText as con(SQLText," AND ",ColName," <> 0")
   Else
      Calculate SQLText as con("WHERE ",ColName," <> 0")
   End If
   
   Calculate SQLText as con(SQLText,' ',OrderBy)
   
   ; Select all the records in the table.
   Do $cinst.$select(SQLText) Returns FlagOK
   If not(FlagOK)
      Calculate Mssg as con("Flag false after $cinst.$select(",SQLText,") for the $sqlclassname ",$cinst.$sqlclassname,".")
      Do errhndlr.$logError($cmethod,Mssg)
   Else
      
      ; Fetch all the records in the table.
      Do $cinst.$fetch(kFetchAll) Returns FetchStatus
      If not(FetchStatus)
         Calculate FlagOK as kFalse
         Calculate Mssg as con("Flag false after $cinst.$fetch(kFetchAll) for the $sqlclassname ",$cinst.$sqlclassname,".")
         Do errhndlr.$logError($cmethod,Mssg)
      Else
         
         ; Set the current line to the first line.
         Do $cinst.$line.$assign(1)
         
      End If
   End If
End If
Quit method FlagOK

Add LIKE to Remote Task

We need to modify the $construct method of the rtCountryList remote task to get countries which begin with the search value entered in the web form by the user. We will prepare a WHERE clause which uses the LIKE operator and call the $getWhere method of the table class.

  1. Go to the $construct method of the rtCountryList remote form.
  2. Add a local variable, Value, character data type.
  3. Add a local variable, SQLText, character data type.
  4. Replace the $getAllRecords section of the method so that the method reads as follows:

; Set the task variables.
Do method setTaskVars Returns FlagOK
If FlagOK
   
   ; Use the upper case value of the first character they entered.
   Calculate SearchValue as upp(mid(pParams.SearchValue,1,1))
   
   ; Define a list variable using the 'tCountry' table class.
   Do List.$definefromsqlclass('tCountry')
   
   ; Set the session object in the list variable so that the SQL statements will be issued to that session's database.
   Do List.$sessionobject.$assign(dbsessionobj)
   
   ; Prepare the WHERE clause.
   Calculate SQLText as con("WHERE CountryName LIKE '",SearchValue,"%'")
   
   ; Select and fetch the records beginning with the search value.
   Do List.$getSelect(SQLText) Returns FlagOK
   If FlagOK
      
      ; Prepare HTML table to return to the web browser.
      Calculate bInclCheckboxes as kTrue
      Calculate CSVColHeadings as 'Country Name'
      Calculate CSVColNames as 'CountryName'
      Calculate PKeyColName as List.$:PrimaryKeyColName
      Do ioHTMLTools.$convertListToHTMLTable(List,bInclCheckboxes,CSVColNames,CSVColHeadings,PKeyColName) Returns TableHTML
      
      ; Get an HTML page template.
      Do ioHTMLTools.$retHTMLPageTemplate() Returns HTML
      
      ; Replace the placeholders with content.
      Calculate HTML as replaceall(HTML,'##TITLE##','Countries')
      Calculate HTML as replaceall(HTML,'##BODY##',TableHTML)
      
      ; Add the HTTP content header.
      Do ioHTMLTools.$addHTTPContentHeader(HTML) Returns FlagOK
      
   End If
End If

If not(FlagOK)
   
   ; An error occurred. Get the last error as an HTML page.
   Do ioHTMLTools.$retLastErrorHTML() Returns HTML
   
End If

Quit method HTML

Test Search LIKE

Test searching for a country that begins with a letter.

  • Open the FireFox web browser.

  • Load the web page. http://localhost/searchcountries.htm

  • Enter U in the search field, or the first letter or any country you know to be in the database.

  • Click the Search button. All going well the list of all the countries beginning with the letter you specified will be returned to the web brower.