JQuery Helpers
AjaxLink Helper
The AjaxLink helper uses jQuery's ajax capabilities to offer the creation of links that do ajax requests and
inject the response into a chosen DOM element. It also offers the possibility to append simple jQuery effects
to both the link and the response DOM element. A simple example introduces its functionality:
view->ajaxLink("Link Name",
"url.php",
array('update' => '#container')); ?>
]]>
This example creates a link with the label "Link Name" that fires an ajax request to url.php
upon click and renders the response into the div container "#container". The function header
for the ajaxLink is as follows: function ajaxLink($label, $url, $options, $params);
The options array is very powerful and offers you lots of functionality to customize your
ajax requests.
Available options are:
AjaxLink options
Option
Data Type
Default Value
Description
update
string
false
Container to inject response content into, use jQuery CSS Selector syntax, ie. "#container" or ".box"
method
string
Implicit GET or POST
Request method, is implicitly chosen as GET when no parameters given and POST when parameters given.
complete
string
false
Javascript callback executed, when ajax request is complete. This option allows for shortcut effects, see next section.
beforeSend
string
false
Javascript callback executed right before ajax request is started. This option allows for shortcut effects, see next section.
noscript
boolean
true
If true the link generated will contain a href attribute to the given link for non-javascript enabled browsers. If false href will resolve to "#".
dataType
string
html
What type of data is the Ajax Response of? Possible are Html, Text, Json. Processing Json responses has to be done with custom "complete" callback functions.
attribs
array
null
Additional HTML attributes the ajaxable link should have.
title, id, class
string
false
Convenience shortcuts for HTML Attributes.
inline
boolean
false
Although far from best practice, you can set javascript for this link inline in "onclick" attribute.
To enlighten the usage of this helper it is best to show another bunch of more complex examples. This
example assumes that you have only one view object that you want to display and don't care a lot about
html best practices, since we have to output the jQuery environment just before the closing body tag.
Zend Framework jQuery AjaxLink Example
-
ajaxLink("Example 1",
"/ctrl/action1",
array('update' => '#content',
'noscript' => false,
'method' => 'POST')); ?>
-
ajaxLink("Example 2",
"/ctrl/action2",
array('update' => '#content',
'class' => 'someLink'),
array('param1' => 'value1',
'param2' => 'value2')); ?>
- ajaxLink("Example 3",
"/ctrl/action3",
array('dataType' => 'json',
'complete' =>
'alert(data)')); ?>
- ajaxLink("Example 4",
"/ctrl/action4",
array('beforeSend' => 'hide',
'complete' => 'show')); ?>
-
ajaxLink("Example 5",
"/ctrl/action5",
array(
'beforeSend' =>
'myBeforeSendCallbackJsFunc();',
'complete' =>
'myCompleteCallbackJsFunc(data);')
); ?>
jQuery(); ?>