Try OpenEdge Now
skip to main content
Developing WebSpeed Applications
Web Objects : Embedded SpeedScript examples : Handling form input

Handling form input

The following shows the section of w-sstget.html, which uses embedded SpeedScript to handle input from an HTML form:


. . .
<h1>Customer Information</h1>
IF get-value("CustNum") NE "" THEN
  FIND Customer
    WHERE Customer.CustNum = INTEGER(get-value("CustNum"))NO-ERROR. -->
<form action="w-sstget.html" method="POST">
  Enter Customer ID: <input name="CustNum" size="5"
    value=‘IF AVAILABLE(Customer) THEN STRING(Customer.CustNum) ELSE ""‘/>
    <input type="SUBMIT" name="Button" value="Find"/>
  IF AVAILABLE(Customer) THEN DO: -->
  Name: <input name="Name" size="20" value=" ‘Customer.Name‘ "/>
  --------Phone: <input name="Phone" size="20" value=" ‘Customer.Phone‘ "/>
  Address 1: <input name="Address" size="20" value=" ‘Customer.Address‘ "/>
  Address 2: <input name="Address2" size="20" value=" ‘Customer.Address2‘ "/>
  City: <input name="City" size="12" value=" ‘Customer.City‘ "/>
  ---State: <input name="State" size="20" value=" ‘Customer.State‘ "/>
  ---Zip: <input name="PostalCode" size="10"
    value=" ‘Customer.PostalCode‘ "/>
  Comments: <textarea name="Comments" rows="2" cols="30">
  ‘Customer.Comments‘ </textarea>
  END. -->
This HTML file uses a different set of statement escape tags (<!--WSS ... -->) than the previous example, but they have the same effect.
The first IF...THEN statement invokes the FIND statement to read a customer record if a customer number (CustNum) is entered by the user. This user enters a customer number in the Web page initially generated by the Web object, as shown in the following figure.
Figure 4. User input page generated by w-sstget
This input is tested by the get-value function, which is one of the WebSpeed API functions that facilitate IO between Web pages and Web objects. The get-value function returns the value of a named element passed with a request, including a form element, a query string element, a cookie, or a user field (an internally defined list value). In this case, the function looks for the value of the CustNum form field defined in the HTML.
For more information on WebSpeed API functions, see WebSpeed API Reference. You can also access the WebSpeed API Reference from the AppBuilder online help. Choose Help® Help Topics from the AppBuilder menu bar. Then select the Find tab and enter WebSpeed API in the top field of the dialog box. Also see WebSpeed API functions for an overview of some commonly used API functions.
Note: The NO-ERROR option on the FIND statement allows the Web object to continue execution if the FIND statement fails because the CustNum value is invalid.
The HTML for the CustNum form element (field) also includes an expression escape tag to provide the field value. The expression contains the SpeedScript IF...THEN...ELSE function, which returns a value based on a condition. Here, the function tests whether the Web object found a customer record by returning the value of the SpeedScript AVAILABLE function on the Customer table (TRUE or FALSE). The AVAILABLE function value is always FALSE for the initial request and a null string value is returned for the IF...THEN...ELSE function.
The second IF...THEN statement also checks AVAILABLE(Customer), and generates HTML to display several Customer field values only if there is a customer record available. This second IF...THEN statement encloses the relevant HTML in a DO block terminated by an END statement to ensure that all the HTML is generated for the available customer record.
The following figure shows the HTML output as it would appear in a browser.
Figure 5. Customer information generated by w-sstget
Note: Although expression escape tags output all SpeedScript expressions as character strings, the values that combine to make a SpeedScript expression can have other data types, such as integer. SpeedScript requires compatible data types in expressions. In the example, the SpeedScript INTEGER function converts the character value of the get-value API function to an integer in order to allow comparison with the integer CustNum field in the Customer table. Similarly, the SpeedScript STRING function converts the integer value of CustNum to a character string in the IF...THEN...ELSE function, because the alternative values for the function must have compatible data types.
As these examples show, you can achieve a fine degree of control over HTML generation within an embedded SpeedScript Web object.