The ajax function

In web2py.js, web2py defines a function called ajax which is based on, but should not be confused with, the jQuery function $.ajax. The latter is much more powerful than the former, and for its usage, we refer you to ref.[jquery] and ref.[jquery-b]. 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:

  1. ajax(url, [name1, name2, ...], target, options)

It asynchronously calls the url (first argument), passes the values of the field inputs with the name equal to one of the names 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:

  1. def one():
  2. return dict()
  3. def echo():
  4. return request.vars.name

and the associated “default/one.html” view:

  1. {{extend 'layout.html'}}
  2. <form>
  3. <input name="name" onkeyup="ajax('{{=URL('default', 'echo')}}', ['name'], 'target')" />
  4. </form>
  5. <div id="target"></div>

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 name="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.

You can also pass options (the fourth argument) directly into the internal $.ajax call. This is useful in the event that a fail callback is required, or a content type must be specified.

Eval target

The third argument of the ajax function can be the string “:eval”. This means that the string returned by server will not be embedded in the document but it will be evaluated instead.

Here is an example of a default controller:

  1. def one():
  2. return dict()
  3. def echo():
  4. return "jQuery('#target').html(%s);" % repr(request.vars.name)

and the associated “default/one.html” view:

  1. {{extend 'layout.html'}}
  2. <form>
  3. <input name="name" onkeyup="ajax('{{=URL('default', 'echo')}}', ['name'], ':eval')" />
  4. </form>
  5. <div id="target"></div>

This allows for more complex responses that can update multiple targets.

Auto-completion

Web2py contains a built-in autocomplete widget, described in the Forms chapter. Here we will build a simpler one from scratch.

Another application of the above ajax function is auto-completion. Here we wish to create an input field that expects a month name and, when the visitor types an incomplete name, performs auto-completion via an Ajax request. In response, an auto-completion drop-box appears below the input field.

This can be achieved via the following default controller:

  1. def month_input():
  2. return dict()
  3. def month_selector():
  4. if not request.vars.month: return ''
  5. months = ['January', 'February', 'March', 'April', 'May',
  6. 'June', 'July', 'August', 'September' , 'October',
  7. 'November', 'December']
  8. month_start = request.vars.month.capitalize()
  9. selected = [m for m in months if m.startswith(month_start)]
  10. return DIV(*[DIV(k,
  11. _onclick="jQuery('#month').val('%s')" % k,
  12. _onmouseover="this.style.backgroundColor='yellow'",
  13. _onmouseout="this.style.backgroundColor='white'"
  14. ) for k in selected])

and the corresponding “default/month_input.html” view:

  1. {{extend 'layout.html'}}
  2. <style>
  3. #suggestions { position: relative; }
  4. .suggestions { background: white; border: solid 1px #55A6C8; }
  5. .suggestions DIV { padding: 2px 4px 2px 4px; }
  6. </style>
  7. <form>
  8. <input type="text" id="month" name="month" style="width: 250px" /><br />
  9. <div style="position: absolute;" id="suggestions"
  10. class="suggestions"></div>
  11. </form>
  12. <script>
  13. jQuery("#month").keyup(function(){
  14. ajax('{{=URL('default', 'month_selector')}}', ['month'], 'suggestions')});
  15. </script>

The jQuery script in the view triggers the Ajax request each time the visitor types something in the “month” input field. The value of the input field is submitted with the Ajax request to the “month_selector” action. This action finds a list of month names that start with the submitted text (selected), builds a list of DIVs (each one containing a suggested month name), and returns a string with the serialized DIVs. The view displays the response HTML in the “suggestions” DIV. The “month_selector” action generates both the suggestions and the JavaScript code embedded in the DIVs that must be executed when the visitor clicks on each suggestion. For example when the visitor types “M” the callback action returns:

  1. <div>
  2. <div onclick="jQuery('#month').val('March')"
  3. onmouseout="this.style.backgroundColor='white'"
  4. onmouseover="this.style.backgroundColor='yellow'">March</div>
  5. <div onclick="jQuery('#month').val('May')"
  6. onmouseout="this.style.backgroundColor='white'"
  7. onmouseover="this.style.backgroundColor='yellow'">May</div>
  8. </div>

Here is the final effect:

image

If the months are stored in a database table such as:

  1. db.define_table('month', Field('name'))

then simply replace the month_selector action with:

  1. def month_input():
  2. return dict()
  3. def month_selector():
  4. if not request.vars.month:
  5. return ''
  6. pattern = request.vars.month.capitalize() + '%'
  7. selected = [row.name for row in db(db.month.name.like(pattern)).select()]
  8. return ''.join([DIV(k,
  9. _onclick="jQuery('#month').val('%s')" % k,
  10. _onmouseover="this.style.backgroundColor='yellow'",
  11. _onmouseout="this.style.backgroundColor='white'"
  12. ).xml() for k in selected])

jQuery provides an optional Auto-complete Plugin with additional functionalities, but that is not discussed here.

Ajax form submission

Here we consider a page that allows the visitor to submit messages using Ajax without reloading the entire page. Using the LOAD helper, web2py provides a better mechanism for doing it than described here, which will be described in Chapter 12. Here we want to show you how to do it simply using jQuery.

It contains a form “myform” and a “target” DIV. When the form is submitted, the server may accept it (and perform a database insert) or reject it (because it did not pass validation). The corresponding notification is returned with the Ajax response and displayed in the “target” DIV.

Build a test application with the following model:

  1. db = DAL('sqlite://storage.sqlite')
  2. db.define_table('post', Field('your_message', 'text'))
  3. db.post.your_message.requires = IS_NOT_EMPTY()

Notice that each post has a single field “your_message” that is required to be not-empty.

Edit the default.py controller and write two actions:

  1. def index():
  2. return dict()
  3. def new_post():
  4. form = SQLFORM(db.post)
  5. if form.accepts(request, formname=None):
  6. return DIV("Message posted")
  7. elif form.errors:
  8. return TABLE(*[TR(k, v) for k, v in form.errors.items()])

The first action does nothing other than return a view.

The second action is the Ajax callback. It expects the form variables in request.vars, processes them and returns DIV("Message posted") upon success or a TABLE of error messages upon failure.

Now edit the “default/index.html” view:

  1. {{extend 'layout.html'}}
  2. <div id="target"></div>
  3. <form id="myform">
  4. <input name="your_message" id="your_message" />
  5. <input type="submit" />
  6. </form>
  7. <script>
  8. jQuery('#myform').submit(function() {
  9. ajax('{{=URL('new_post')}}',
  10. ['your_message'], 'target');
  11. return false;
  12. });
  13. </script>

Notice how in this example the form is created manually using HTML, but it is processed by the SQLFORM in a different action than the one that displays the form. The SQLFORM object is never serialized in HTML. SQLFORM.accepts in this case does not take a session and sets formname=None, because we chose not to set the form name and a form key in the manual HTML form.

The script at the bottom of the view connects the “myform” submit button to an inline function which submits the INPUT with id="your_message" using the web2py ajax function, and displays the answer inside the DIV with id="target".

Voting and rating

Another Ajax application is voting or rating items in a page. Here we consider an application that allows visitors to vote on posted images. The application consists of a single page that displays the images sorted according to their vote. We will allow visitors to vote multiple times, although it is easy to change this behavior if visitors are authenticated, by keeping track of the individual votes in the database and associating them with the request.env.remote_addr of the voter.

Here is a sample model:

  1. db = DAL('sqlite://images.db')
  2. db.define_table('item',
  3. Field('image', 'upload'),
  4. Field('votes', 'integer', default=0))

Here is the default controller:

  1. def list_items():
  2. items = db().select(db.item.ALL, orderby=db.item.votes)
  3. return dict(items=items)
  4. def download():
  5. return response.download(request, db)
  6. def vote():
  7. item = db.item[request.vars.id]
  8. new_votes = item.votes + 1
  9. item.update_record(votes=new_votes)
  10. return str(new_votes)

The download action is necessary to allow the list_items view to download images stored in the “uploads” folder. The votes action is used for the Ajax callback.

Here is the “default/list_items.html” view:

  1. {{extend 'layout.html'}}
  2. <form><input type="hidden" id="id" name="id" value="" /></form>
  3. {{for item in items:}}
  4. <p>
  5. <img src="{{=URL('download', args=item.image)}}"
  6. width="200px" />
  7. <br />
  8. Votes=<span id="item{{=item.id}}">{{=item.votes}}</span>
  9. [<span onclick="jQuery('#id').val('{{=item.id}}');
  10. ajax('{{=URL('default', 'vote')}}', ['id'], 'item{{=item.id}}');">vote up</span>]
  11. </p>
  12. {{pass}}

When the visitor clicks on “[vote up]“ the JavaScript code stores the item.id in the hidden “id” INPUT field and submits this value to the server via an Ajax request. The server increases the votes counter for the corresponding record and returns the new vote count as a string. This value is then inserted in the target item{{=item.id}} SPAN.

Ajax callbacks can be used to perform computations in the background, but we recommend using cron or a background process instead (discussed in chapter 4), since the web server enforces a timeout on threads. If the computation takes too long, the web server kills it. Refer to your web server parameters to set the timeout value.