Solved

How to get a PHP param inside a JS file

Posted on 2011-02-16
9
305 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 108

Expert Comment

by:Ray Paseur
Comment Utility
Probably something like this:

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

Author Comment

by:trrsrr
Comment Utility
@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
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Expert Comment

by:JayDiablo
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
@onemadeye:
Glad to know someone ever been in the same case :)

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now