Solved

How to get a PHP param inside a JS file

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

21 Experts available now in Live!

Get 1:1 Help Now