Solved

How to get a PHP param inside a JS file

Posted on 2011-02-16
9
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 110

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How difficult would it be build a cookie scanner in PHP? 21 62
phpmyadmin memory error 55 95
Error Handler 8 43
Add Promise to Existing Code 6 31
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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…

738 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