Question

function move(){
  var move= <?php echo $config['root'];?>
    window.location.href=""+move+"sf/" ;
}

I have config.php and all the root variable is defined in it, I want to move the user on click to that URL,

  1. Problem is that I can't access the variable from config.php
  2. Its not good to create an index.js file as well.
  3. As the code moves between Prod, QA and Dev so the URL changes for each environment, I want that I only change at one point and dont have to make changes at all code. The above code is also not working .
Was it helpful?

Solution

What JS variable are you trying to access in PHP? It looks to me like you're trying to use PHP in your JS, not the other way around.

I think you just need to put quotes around your PHP output like this:

var move = "<?php echo $config['root']; ?>";

Then you can simply do this:

window.location.href = move;

OTHER TIPS

1- Problem is that I can't access the variable from config.php well, this code should work var move= <?php echo '"'.$config['root'].'"';?>

Please notice that an URL is a string. You can not write something as var string = some text

instead, you should add quotation marks at the beginning and end of the string. var string = "some text"

I just don't know what you mean with the first 2 quotation marks in ""+move+"sf/" ;

Did you hide the domain? If so, I suggest you to add this part of the URL in the config.php as well.

3- I want that I only change at one point and dont have to make changes at all code.

But the point is config.php, right? You just have to change the variables in config.php and make sure all the other .PHP files include it.

have a look at the php.ini option called auto_prepend_file to automatically add PHP code to all PHP scripts.

http://www.php.net/manual/en/ini.core.php#ini.auto-prepend-file

you have to provide a local path (/usr/local/... or c:/server/...)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top