Log In    |    Knowledge Base    |    4D Home
Tech Tip: Handling an unknown number of Variables on a web page
Published On: August 28, 1999

In 4D v6.5, when accessing values of objects from Web pages, it is necessary to declare the web page variables within the COMPILER_WEB method. When the Web server receives a posted form, it calls the COMPILER_WEB method. 4D will then analyze the HTML objects in the form, receive their values, and will fill like-named 4D variables with their contents.

In situations where you are not sure of the number of objects on a form, it will be impossible to declare the variables in the COMPILER_WEB method. This causes a problem because 4D will generate a runtime error if you try to access an undefined variable.

Suppose you want 4D to dynamically generate an HTML table that displays a list of products. For each product, you generate a corresponding HTML checkbox. The idea is that the user will be able to select the checkbox to receive more information about the particular product. The problem exists in that the number of products is constantly changing; therefore the number of checkbox variables is also changing. Due to the changing number of checkboxes, it is impractical to declare them in the COMPILER_WEB method. Here's how to manage the checkboxes:

When generating the checkboxes give them a common name, plus a value. For example, "ProdCheck1, ProdCheck2, etc..."

When getting the Web page back via a Post action, perform a loop that validates the checkbox as defined. Checkboxes that are not checked, and not declared in COMPILER_WEB, will be returned to the 4D method as undefined. If the checkbox is checked it will be defined and have the appropriate value. As long as the variable is defined, 4D will not generate a runtime error.

Note: This procedure will only work with uncompiled databases.

The following code shows this process:

Cut and Paste the following code example into your
own 4D project

`Check to see if the check boxes are defined, ie "Checked"
For($i;1;Records in selection([Products]))
pAns:=Get pointer("Cart"+String(aProductID{$i}))

if (Not(Undefined(pAns->)))
End if

End for