Page Variables

Description

Data can be passed to a web page in the form of page variables in the URL. For example, "myapp.a5w?custId=000001". These page variables can be referenced in server-side events and Ajax Callbacks in components.

Discussion

You can reference page variables in the events in a component through the context.request.variables object. For example, assume that you have a page that is loaded using this URL:

myDialogPage.a5w?custId=000001

Assume that this page contains a UX component. When the page is run, a variable called custID is created with a value of "BOLID" and added to the context.request.variables object. Inside the UX component's onDialogInitialize event, you can reference this variable to perform a server-side calculation. For example:

function onDialogInitialize as v (e as p)
    
    ' Test to make sure the page variable exists before referencing it
    if (variable_exists("context.request.variables.custId")) then
        dim custId as C = context.request.variables.custId
        dim cn as SQL::Connection
        if (cn.open("name::AADemo-Northwind"))
            dim args as SQL::Arguments
            args.set("WhatCustomer",custId)
            dim sql as c =<<%sql%
SELECT CompanyName, ContactName, Phone, Country FROM Customers WHERE CustomerId = :WhatCustomer
%sql%
            if (cn.execute(sql,args))
                dim records[0] as p 
                
                if (cn.resultSet.toPropertyArray(records) .and. records.size() > 0) then
                    e.control.CompanyName = records[1].CompanyName
                    e.control.ContactName = records[1].ContactName
                    e.control.Phone = records[1].Phone
                    e.control.Country = records[1].Country
                else
                    ' no records or error retrieving to property array
                end if
            else
                ' Error executing statement
                ' Check cn.callResult.text to learn why
            end if
        else 
            ' Error connecting to DB
            ' Check cn.callResult.text to learn why
        end if
    else
        ' Page variable called "custId" was not defined
        ' IE, ?custId=BOLID was not included in the URL
    end if

end function

There are several things to know about working with Page Variables:

  • When referencing page variables in Xbasic, you must check that the page variable exists before you use it. This is done by using the variable_exists() Xbasic function.
  • Be wary of using page variables without checking their value. They are easily abused by others and pose a security risk if not properly sanitized.
  • You can create Arguments in the component that are populated from page variables. These arguments can be used throughout components, in SQL filters, or other places (e.g. setting control values) that don't require any coding.
  • Page variables should never be used directly in SQL statements. You should always use SQL::Arguments when using page variables with SQL queries.
  • Page variables can also be referenced in client-side JavaScript.

See Also