Jump to content

Importing a Javascript into PHP code.


Drawn

Recommended Posts

Hello, all.

 

I've got a function intoducing some javascript to a webpage:

 

$ajaxClass = self::getAJAXClass();

 

$ajaxMethod = self::getAJAXMethod();

 

$script = self::getAJAXScript();

 

$row->text .= '<script language="javascript"  type="text/javascript">' .....blah, blah, blah  (see code below)

 

The object, $row, eventually is outputted to the webpage, and this code works perfectly.

function onBeforeDisplayContent( &$row, &$params, $page=0 ){

	static $already;

	// Already been called.
	if ( isset($already) ) return true;

	$already = self::ajaxReady();

	// Retreat if not ready.
	if ( !$already ) return true;

	$pluginDir = self::getAjaxRootDir();

	$ajaxServer = $pluginDir . 'ajax_server.php';

	$ajaxID = self::getAJAXid();

	$ajaxFile = self::getAJAXFile();

	$ajaxClass = self::getAJAXClass();

	$ajaxMethod = self::getAJAXMethod();

// #################  Here's the issue.  An ugly long string assignment.  ####
	$row->text .= '<script language="javascript"  type="text/javascript">' .

	 'HTML_AJAX.defaultServerUrl = "'. $ajaxServer . '";


  function clearTarget(){

  document.getElementById("'. $ajaxID . '").innerHTML = "clear";
  }

  // Add an error handler so we get an alert if any errors happen while making an AJAX call
  HTML_AJAX.onError = function(e){

  alert(HTML_AJAX_Util.varDump(e));
  }


  // It is this function that is called within the HTML document.
  function clickSource(param){
// #################  Note that php variables are concatenated within the string. ####	   
  file = "' . $ajaxFile . '";

  class = "' . $ajaxClass . '";

  method = "' . $ajaxMethod . '";

  callAsync(file, class, method, param);
  }


  function callAsync(file, class, method, param){

  HTML_AJAX.call(file, class, method, callCallback, param);
  }


  function callCallback(result){

  document.getElementById("'. $ajaxID . '").innerHTML = result;
  }
  </script>';
}

             return true;

As you can see, the attribute, $row->text, is just being assigned a long ugly string composed of a javascript functions enclosed in the <SCRIPT> </SCRIPT> tags.

 

Now I want my users (fellow Joomla! programmers) to conceive their own scripts! This function will then simply take in that script:

function onBeforeDisplayContent( &$row, &$params, $page=0 ){

	static $already;

	// Already been called.
	if ( isset($already) ) return true;

	$already = self::ajaxReady();

	// Retreat if not ready.
	if ( !$already ) return true;

	$pluginDir = self::getAjaxRootDir();

	$ajaxServer = $pluginDir . 'ajax_server.php';

	$ajaxID = self::getAJAXid();

	$ajaxFile = self::getAJAXFile();

	$ajaxClass = self::getAJAXClass();

	$ajaxMethod = self::getAJAXMethod();

                          // Retrieves a string from an external file.
	$script = self::getAJAXScript();

// ### Something magical happens here!  The script is appended and outputted.
	$row->text .= self::eval_html($script);

                          return true;
}

 

 

'Something magical happens here.' is my troubles.

 

I want the users to reference the variables, $ajaxFile, $ajaxClass, $ajaxMethod, in their 'javascript code' file.

 

I've made some unsuccessful attempts.  (eg. $row->text .= eval("\$script = $script;");  )

 

Do you see what I'm going after?

 

Ideally I would want the user to simply create a file ( ajax_script.php for example ) that contains just the javascript commands and functions:

 

ajax_script.php:

HTML_AJAX.defaultServerUrl = "'. $ajaxServer . '";


  function clearTarget(){

  document.getElementById("'. $ajaxID . '").innerHTML = "clear";
  }

  // Add an error handler so we get an alert if any errors happen while making an AJAX call
  HTML_AJAX.onError = function(e){

  alert(HTML_AJAX_Util.varDump(e));
  }


  // It is this function that is called within the HTML document.
  function clickSource(param){
   
  file = "' . $ajaxFile . '";

  class = "' . $ajaxClass . '";

  method = "' . $ajaxMethod . '";

  callAsync(file, class, method, param);
  }


  function callAsync(file, class, method, param){

  HTML_AJAX.call(file, class, method, callCallback, param);
  }


  function callCallback(result){

  document.getElementById("'. $ajaxID . '").innerHTML = result;
  }
</script>';

 

I would then put in the appropriate <SCRIPT> </SCRIPT> tags.

 

Heck! Why does the user even have to compose a string in that file?!

 

Why not just create a javascript file, ajax_script.js, with the html tags and all (including the reference to the php variables):

 

ajax_script.js:

 HTML_AJAX.defaultServerUrl = "<?php $ajaxServer ?>";


  function clearTarget(){

     document.getElementById("<?php $ajaxID ?>").innerHTML = "clear";
      }

      // Add an error handler so we get an alert if any errors happen while making an AJAX call
      HTML_AJAX.onError = function(e){
    
     alert(HTML_AJAX_Util.varDump(e));
      }

      
      // It is this function that is called within the HTML document.
  function clickSource(param){
  
     file = "<?php $ajaxFile ?>";
     
     class = "<?php $ajaxClass ?>";

     method = "<?php $ajaxMethod ?>";

     callAsync(file, class, method, param);
  }


  function callAsync(file, class, method, param){

     HTML_AJAX.call(file, class, method, callCallback, param);
      }

      
      function callCallback(result){

     document.getElementById("<?php $ajaxID ?>").innerHTML = result;
      }

 

 

I'll continue my research and testing.

 

I was just hoping someone else has already done such a thing.

 

I'm not thinking out of the box here!

 

I can only conclude that I have to use eval() in some manner.

 

I was hoping someone has an alternative solution or suggestion.

 

Regards,

Link to comment
https://forums.phpfreaks.com/topic/146003-importing-a-javascript-into-php-code/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.