Helpers::JavascriptHelper::link_to_remote
link_to_remote( string $name, array $options = array(), array $html_options = array() )
Description
Returns a link to a remote action defined by $optionsurl? (using the url_for() format) that’s called in the background using XMLHttpRequest. The result of that request can then be inserted into a DOM object whose id can be specified with $optionsupdate?.
This method requires the inclusion of the script.aculo.us Javascript library.
$name a string which is the text for the href.
$options an array of key => value pairs of javasscript options.
$html_options is an array of key => value pairs of html options for the anchor (a) tag.
This method requires the inclusion of the script.aculo.us Javascript library.
Examples:
<?= link_to_remote("Delete this post", array(
"update" => "posts",
"url" => array(":action" => "delete",
":id" => $post->id))) ?>
<?= link_to_remote(image_tag("refresh"), array(
"update" => "emails",
"url" => array(":action" => "list_emails"))) ?>
You can also specify a hash for $optionsupdate? to allow for easy redirection of output to an other
DOM element if a server-side error occurs:
Example:
<?= link_to_remote("Delete this post", array(
"url" => array(":action" => "delete",
":id" => $post->id),
"update" => array("success" => "posts",
"failure" => "error")) ?>
Optionally, you can use the options[:position] parameter to influence how the target DOM element is updated. It must be one of "before", "top", "bottom", or "after".
By default, these remote requests are processed asynchronous during which various Javascript callbacks can be triggered (for progress indicators and the likes). All callbacks get access to the request object, which holds the underlying XMLHttpRequest.
To access the server response, use request.responseText, to find out the HTTP status, use request.status.
Example:
<?= link_to_remote("word", array(
"url" => array(":action" => "undo"),
"complete" => "undoRequestCompleted(request)") ?>
The callbacks that may be specified are (in order):
- "loading": Called when the remote document is being loaded with data by the browser.
- "loaded": Called when the browser has finished loading the remote document.
- "interactive": Called when the user can interact with the remote document, even though it has not finished loading.
- "success": Called when the XMLHttpRequest is completed, and the HTTP status code is in the 2XX range.
- "failure": Called when the XMLHttpRequest is completed, and the HTTP status code is not in the 2XX range.
- "complete": Called when the XMLHttpRequest is complete (fires after success/failure if they are present).
If you for some reason or another need synchronous processing (that’ll block the browser while the request is happening), you can specify $optionstype? = "synchronous".
You can customize further browser side call logic by passing in Javascript code snippets via some optional parameters. In their order of use these are:
- "confirm": Adds confirmation dialog.
- "condition": Perform remote request conditionally by this expression. Use this to describe browser-side conditions when request should not be initiated.
- "before": Called before request is initiated.
- "after": Called immediately after request was initiated and before "loading".
- "submit": Specifies the DOM element ID that’s used as the parent of the form elements. By default this is the current form, but it could just as well be the ID of a table row or any other DOM element.

