The ajax Function

In web2py_ajax.html, web2py defines a function called ajax which is based on, but should not be confused with, the jQuery function $ . aj ax. The latter is much more powerful than the former, and for its usage, we refer you to ref. [31] and ref. [68]. However, the former function is sufficient for many complex tasks, and is easier to use.

The ajax function is a JavaScript function that has the following syntax:

It asynchronously calls the url (first argument), passes the values of the fields with the id equal to one of the ids in the list (second argument), then stores the response in the innerHTML of the tag with the id equal to target (the third argument).

Here is an example of a default controller:

2 return dict()

5 return request.vars.name and the associated "default/one.html" view:

3 <input id="name" onkeyup= "'ajax('echo', ['name'], 'target')" />

When you type something in the INPUT field, as soon as you release a key (onkeyup), the ajax function is called, and the value of the i d="name" field is passed to the action "echo", which sends the text back to the view. The ajax function receives the response and displays the echo response in the "target" DIV.

Was this article helpful?

+2 0

Post a comment