Tips_tutorials   >   Studiojs201   >   Keeping the Search Form
We have a problem with our user interface in that the search form (search field and button) is lost when the results are returned to the web brower.
There are a number of ways we can solve solve this:
The problem with solution #1 is that the search form HTML in our Omnis Studio application has to exactly match the search form HTML in the static page or the search form on the results page won't match the look of the static page. Whenever a change is made to the static page, the HTML in the Omnis Studio application will have to be update. That gets to be a headache as we add more and more pages.
Solution #3 requires a fair bit of JavaScript and DHTML (Dynamic HTML) knowledge. Not something we want to tackle at this stage in the tutorial.
Solution #2 is the one we will pursue.This is where the fun begins. Be prepared for some hair pulling adventures. You might want to take a coffee break before you dive into this section. We are going to have to tackle both HTML frames and JavaScript to get this working.
Our first baby step into the JavaScript world will be to add a Ping Page alert to our web page.
<script>
function pingPage() {
alert("Ping Page");
}
</script>
This tutorial is not intended to teach you JavaScript syntax. There is a plethora of information about JavaScript on the internet. The
, recommended earlier in this tutorial, is excellent an excellent reference manual for JavaScript.<input type="button" value="Ping Page" onclick="pingPage()" />
Putting loads of JavaScript in HTML files is not a good idea. In fact, you probably shouldn't put any JavaScript in your HTML files. Instead put all of your JavaScript in .js files and then load and call the from your HTML files.
function pingFile() {
alert("Ping File");
}
If the Ping File alert did not appear use the FireFox
to debug your code.Having a JavaScript ping function is handy for testing communication. If all else fails, add a ping function and button to your page and test to make sure that at least it works.
As we have learned earlier in this tutorial, when we clicked the
button, the input values inside the <form> were sent as parameters to the web server using the method and action specified by the form. The request was then processed by the Omnis Web App Server. The entire contents of the web page which sent the request to the web server was replaced.Instead of submiting the search form from the main page, we are going to gather the information from the search form using JavaScript and then send a request to the web server via the lower frame. That way the HTML returned by our Omnis Studio web app will fill the frame, rather than the whole page.
We'll do this one step at a time, and test each step as we go.function submitSearchViaFrame() {
alert("Start submitSearchViaFrame");
}
Working with JavaScript it is a good practice to write a small chunk of code and test it, then write some more, and test again. Debugging JavaScript is a bear, so you want to find and fix errors one small step at a time. Remember to use the
to debug your code. It will tell you the location of the first error it hits.Now add the code to the submitSearchViaFrame function which gathers the information are going to need.
function submitSearchViaFrame() {
//alert("Start submitFormViaFrame");
// Get a reference to the Search Form
var rForm = document.SearchForm ;
// Get the action property from the form.
var URL = rForm.action
alert("URL = " + URL);
}
You can comment out code in JavaScript using the // double slash charaters.
Now we'll build the full URL which we want to point the frame to.
function submitSearchViaFrame() {
//alert("Start submitFormViaFrame");
// Get a reference to the Search Form
var rForm = document.SearchForm ;
// Get the action property from the form.
var URL = rForm.action
alert("URL = " + URL);
// Get the search value from the form's entry field.
var SearchValue = rForm.SearchValue.value
alert("SearchValue = " + SearchValue);
var OmnisServer = rForm.OmnisServer.value
alert("OmnisServer = " + OmnisServer);
var OmnisLibrary = rForm.OmnisLibrary.value
alert("OmnisLibrary = " + OmnisLibrary);
var OmnisClass = rForm.OmnisClass.value
alert("OmnisClass = " + OmnisClass);
URL = URL + "?" + "OmnisServer=" + OmnisServer + "&OmnisLibrary=" + OmnisLibrary + "&OmnisClass=" + OmnisClass + "&SearchValue=" + SearchValue;
alert("URL = " + URL);
}
The final step is to point the frame to the URL causing it to send an HTTPRequest to the web server. The frame's src (source) property is what points it to a specific web page. All we need to do is set the src property to the URL and we will taste the sweet victory of success.
// Get a reference to the frame
var rFrame = document.getElementById("SearchResults");
alert("Frame ID = " + rFrame.id);
// Point the frame to the URL
rFrame.src = URL ;
Congratulations! You have an ultra-thin client Omnis Studio application up and running! Go ahead and test if from other web browsers.
Unfortunately, Internet Explorer is not a fully W3C compliant. There are a few little JavaScript glitches that you will run into between FireFox, Internet Explorer, and Safari. If you run into one of these, do a Google search and you'll likely be able to find a work around.
The <form> on the static page isn't actually needed. On my web apps I usually put all of the hidden inputs at the bottom of the <body> section of the web page. I also add a hidden input called WebAppServerCGI with its value set to the URL path to the Omnis CGI or Script.
Eliminating the form and moving the inputs to the bottom of the page, makes for a very simple HTML page.
Here is what the cleaned up HTML page looks like:
<html>
<head>
<title>Countries</title>
<script type="text/javascript" src="../js/countries.js"></script>
</head>
<body>
<h3>Countries</h3>
<p>Enter the first letter of a country and click the Search button</p>
<input type="text" id="SearchValue" name="SearchValue" size="1" />
<input type="button" value="Search" onclick="submitSearchViaFrame()" />
<iframe id="SearchResults" name="SearchResults" src="" width="100%" height="100%" scrolling="yes"></iframe>
<input type="hidden" id="WebAppServerCGI" value="http://localhost/cgi-bin/nph-omniscgi" />
<input type="hidden" id="OmnisServer" value="5912" />
<input type="hidden" id="OmnisLibrary" value="ContactsWeb" />
<input type="hidden" id="OmnisClass" value="rtCountryList" />
</body>
</html>
Here is what the cleaned up submitSearchViaFrame JavaScript function looks like:
function submitSearchViaFrame() {
// Gather the hidden inputs needed to assemble the URL
var CGI = document.getElementById("WebAppServerCGI").value
var OmnisServer = document.getElementById("OmnisServer").value
var OmnisLibrary = document.getElementById("OmnisLibrary").value
var OmnisClass = document.getElementById("OmnisClass").value
var SearchValue = document.getElementById("SearchValue").value
var URL = CGI + "?" + "OmnisServer=" + OmnisServer + "&OmnisLibrary=" + OmnisLibrary + "&OmnisClass=" + OmnisClass + "&SearchValue=" + SearchValue;
//alert("URL = " + URL);
// Get a reference to the frame
var rFrame = document.getElementById("SearchResults");
// Point the frame to the URL
rFrame.src = URL ;
}