Solved

How to get a PHP param inside a JS file

Posted on 2011-02-16
9
310 Views
Last Modified: 2012-05-11
Here's the case, I use FCKeditor with GoogleMaps plugin.
To make this GoogleMaps plugin works properly, I have to insert this string in fckconfig.js (JS file)
FCKConfig.GoogleMaps_Key = 'googleMapsAPIKey' ;

What I want is to be able to configure the GoogleMaps Key in my main configuration file named mainconfig.php (PHP file) like
$googleMapsAPIKey = 'ABQIAAAAnfs7bKE82qgb3Zc2YyS';

So, the question is how the JS file can recognize the needed value that is stored in the PHP file... any help or idea?
0
Comment
Question by:trrsrr
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34908038
Probably something like this:

FCKConfig.GoogleMaps_Key = '<?php echo $googleMapsAPIKey; ?>' ;
0
 
LVL 1

Author Comment

by:trrsrr
ID: 34908101
@Ray_Paseur:
Yes I think I know that way but only if it is calling from a PHP file, rite?
But the FCKConfig.GoogleMaps_Key is in a JS file so Idont think it can recognize <?php
And also how to include the mainconfig.php inside ths JS file?
If it was a PHP file I assumed I'll call it like this at the top of the file:

include("../../../../mainconfig.php");
0
 
LVL 9

Expert Comment

by:wellhole
ID: 34908221
One way you can do it is to create a variable in your html in a script tag prior to loading the js file and then reference that variable in your js file. Another way is to read the js using php, replace the content, spit it out, and remove the old reference to the javascript.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34908268
The FCKeditor is JavaScript, but it has to be loaded into an HTML or PHP page.  I would put that information right at the top of the PHP page so it is available to the PHP script parser.  Something along these lines...
<?php // RAY_php_and_js.php
error_reporting(E_ALL);


// DEMONSTRATE HOW TO PUT SOMETHING FROM A PHP SCRIPT INTO A JAVASCRIPT VARIABLE


date_default_timezone_set('America/Chicago');
$now = 'TODAY IS ' . date('m/d/y');

// END OF PHP
?>

<script type="text/javascript">
var phpdata = '<?php echo $now; ?>';
alert(phpdata);
</script>

Open in new window

0
 
LVL 4

Expert Comment

by:JayDiablo
ID: 34908279
FCK Editor has a way to load it through PHP, so you should be able to define the config variables in PHP instead of in fckconfig.js:

http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Integration/PHP
0
 
LVL 1

Author Comment

by:trrsrr
ID: 34908765
@JayDiablo:
The reason I dont prefer FCKeditor in PHP configuration is because it cannot act as a replacement for <textarea> and will display as <iframe> so I use I choose the JavaScript integration ... beside, I have to modify some many codes on other files too if I have to change the way FCKeditor integrated with my script (yes .. yes.. it's not OOP unfortunately).

@Ray_Paseur:
Here's an example of the PHP file that loads the FCKeditor ... you can see that I tried to call the GoogleMaps key there and it's not working because it has to be called inside the fckconfig.js
<?php
$GoogleMapsAPIKey = 'ABQIAAAAnfs7bKE82qgb3Zc2YyS';
?>
<script type="text/javascript" src="../includes/fckeditor/fckeditor.js"></script>
<script type="text/javascript">
window.onload = function()
{
	var oFCKeditor		= new FCKeditor( 'content' ) ;
	oFCKeditor.BasePath	= '<?php echo URL_INCLUDES; ?>fckeditor/'
	oFCKeditor.Height	= 360 ;
	oFCKeditor.ReplaceTextarea() ;
}

FCKConfig.GoogleMaps_Key = '<?php echo $GoogleMapsAPIKey; ?>' ;
</script>
<form>
<!-- Rest of codes here -->
</form>

Open in new window

0
 
LVL 4

Assisted Solution

by:JayDiablo
JayDiablo earned 100 total points
ID: 34908927
Ok, then just change the order that you're placing the GoogleMaps key config, like so:

<?php
$GoogleMapsAPIKey = 'ABQIAAAAnfs7bKE82qgb3Zc2YyS';
?>
<script type="text/javascript" src="../includes/fckeditor/fckeditor.js"></script>
<script type="text/javascript">
FCKConfig.GoogleMaps_Key = '<?php echo $GoogleMapsAPIKey; ?>' ;

window.onload = function()
{
        var oFCKeditor          = new FCKeditor( 'content' ) ;
        oFCKeditor.BasePath     = '<?php echo URL_INCLUDES; ?>fckeditor/'
        oFCKeditor.Height       = 360 ;
        oFCKeditor.ReplaceTextarea() ;
}
</script>
<form>
<!-- Rest of codes here -->
</form>

Open in new window


That should be the same as if you had put it in the fckconfig.js file (since it's basically doing the same thing).  As long as it's defined before your call to "new FCKeditor" it should see it.

As per the documentation:  http://docs.cksource.com/FCKeditor_2.x/Developers_Guide/Configuration_File


When overriding configurations, the following steps are taken:

   1. The configurations in the main configuration file (fckconfig.js) are loaded.
   2. The configurations are overridden by the settings in the custom configuration file (if provided).
   3. The configurations are finally overridden by the settings done inline in the editor page, except for the "CustomConfigurationsPath", which is set right after step 1.

Step 3 being the approach here.
0
 
LVL 5

Accepted Solution

by:
onemadeye earned 400 total points
ID: 34909870
If you are using this plugin:
http://martinezdelizarrondo.com/googlemaps/

I've been on the same case before and find out that this is the only working trick for that plugin:
http://alfonsoml.blogspot.com/2008/02/adding-configuration-entries-when.html

Regards...
0
 
LVL 1

Author Closing Comment

by:trrsrr
ID: 34911092
@onemadeye:
Glad to know someone ever been in the same case :)

@JayDiablo:
Thanks for your approaches too ... :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question