Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get a PHP param inside a JS file

Posted on 2011-02-16
9
Medium Priority
?
369 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 111

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 111

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 400 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 1600 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

885 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