User input handling is identical in the CGI and Webapp models. You may want to read the WebCommon documentation for more information.
User input handling
User input can come from three sources:
- GET: the query string on the URL, for example ?a=1;b=3 (Kaya name:
DataGet
). - POST: the method usually used for form submissions (Kaya name:
DataPost
). - Cookies: state variables stored semi-persistently in the user’s web browser. (Kaya name:
DataCookie
).
Kaya also defines a Request data source: the union of GET and POST data (if the same key is present in both, POST takes precedence), which is occasionally useful, for example for search forms (Kaya name: DataRequest
). People used to PHP should note that, unlike PHP, this does not include cookie data.
There are three functions for retrieving this data.
Checking for existence
The incomingExists function checks for the existence of a key in the specified data source.
exists = incomingExists("id",DataGet);
// true: ?id=5;option=2
// true: ?option=3&id=4
// true: ?id=3;id=5;id=9;option=2;id=11
// true: ?id
// true: ?id=
// false: ?option=7
Retrieving a single value
The incomingValue retrieves a single value with the specified key from the specified data source. If there are multiple values, it retrieves only the first:
value = incomingValue("id",DataGet);
// "5": ?id=5;option=2
// "4": ?option=3&id=4
// "3": ?id=3;id=5;id=9;option=2;id=11
// "": ?id
// "": ?id=
// throws an Exception: ?option=7
Retrieving a list of values
The incomingData retrieves a list of values with the specified key from the specified data source. This is essential for <select multiple=’multiple’>, and often very useful for sets of checkboxes:
value = incomingData("id",DataGet);
// ["5"]: ?id=5;option=2
// ["4"]: ?option=3&id=4
// ["3","5","9","11"]: ?id=3;id=5;id=9;option=2;id=11
// [""]: ?id
// [""]: ?id=
// throws an Exception: ?option=7
File upload handling
File uploads are by default from 0.2.4 disabled in CGI and webapps. To allow file uploads, call the allowFileUploads function from your CGI or webapp’s webconfig
function.
The incomingFileExists, incomingFile and incomingFiles functions behave in the same way as their counterparts for user data. Files are returned a value of type Uploaded and three functions are defined to retrieve information about this type:
file = incomingFile("upload");
// originalName(file) = original name of file
// tempPath(file) = path on server filesystem to retrieve file
// contentType(file) = content type of file
Files are only available at their temporary path for the duration of the CGI/webapp execution. If they are still there when the program exits, they will be deleted, and so should be copied to another location, or read into a database, if they need to be kept. Files will not be deleted if the program exits early due to a program crash or use of the exit or _exit functions. Obviously, both of these should be avoided!
See the file upload tutorial for more information.