Tips_tutorials   >   Studiojs201   >   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
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:
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.
If you are on the MacOS X platform get
if you don't already have it. It is the programmer's best friend for editing HTML, JavaScript, CSS, PHP, etc.<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>
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.
; 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
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.
; 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 searching for a country that begins with a letter.