views:

323

answers:

1

I'm trying to pull in HTML/Javascript from an external PHP page using Prototype/AJAX, and the HTML renders fine, but the Javascript doesn't work after the AJAX request.

I'm also showing a loading bar while the page loads the data, and this part works fine. My Symfony PHP + Prototype code is below. Any advice that might put me in the right direction would be greatly appreciated!

<?php echo javascript_tag(
      remote_function(array(
      'update'  => 'getinfo',
      'method'    => 'get',
      'script'     => true,
      'url'     => 'event/load?id='.$sf_params->get('id'),
      'loading'    => "Element.show('loading')",
     'complete'   => "Element.hide('loading')",
))
 ) ?>
+2  A: 

From "The Definitive Guide to Symfony":

Even if you enable script execution for the remote response, you won't actually see the scripts in the remote code, if you use a tool to check the generated code. The scripts will execute but will not appear in the code. Although peculiar, this behavior is perfectly normal.

I believe this is because remote_function is implemented using Prototype's Ajax.Updater; remote_function's script option becomes Ajax.Updater's evalScripts option. The docs for Ajax.Updater has this to say:

If you use evalScripts: true, any block will be evaluated. This does not mean it will get included in the page: they won't. Their content will simply be passed to the native eval() function. [... To define a function,] you will need to use the following syntax:

// This kind of script WILL work if processed by Ajax.Updater:
coolFunc = function() {
    // Amazing stuff!
}

This suggests you are experiencing a scoping issue. That's as much as I can say without seeing the javascript output by event.php.

outis