Welcome to the Cookbook

loading...

7.1.2.2 remoteFunction

remoteFunction(array $options);

This function creates the JavaScript needed to make a remote call. It is primarily used as a helper for link(). This is not used very often unless you need to generate some custom scripting.

The $options for this function are the same as for the link method

Example:

<div id="post">
</div>
<script type="text/javascript">
<?php echo $ajax->remoteFunction( 
    array( 
        'url' => array( 'controller' => 'posts', 'action' => 'view', 1 ), 
        'update' => 'post' 
    ) 
); ?>
</script>
  1. <div id="post">
  2. </div>
  3. <script type="text/javascript">
  4. <?php echo $ajax->remoteFunction(
  5. array(
  6. 'url' => array( 'controller' => 'posts', 'action' => 'view', 1 ),
  7. 'update' => 'post'
  8. )
  9. ); ?>
  10. </script>

It can also be assigned to HTML Event Attributes:

<?php 
    $remoteFunction = $ajax->remoteFunction( 
        array( 
        'url' => array( 'controller' => 'posts', 'action' => 'view', 1 ),
        'update' => 'post' ) 
    ); 
?>
<div id="post" onmouseover="<?php echo $remoteFunction; ?>" >
Mouse Over This
</div>
  1. <?php
  2. $remoteFunction = $ajax->remoteFunction(
  3. array(
  4. 'url' => array( 'controller' => 'posts', 'action' => 'view', 1 ),
  5. 'update' => 'post' )
  6. );
  7. ?>
  8. <div id="post" onmouseover="<?php echo $remoteFunction; ?>" >
  9. Mouse Over This
  10. </div>

If $options['update'] is not passed, the browser will ignore the server response.