1
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 .

2 Answers 2

2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

but still cant access the config.php in my js
@user1765876 Then explain what you're trying to access better. right now it doesn't make a whole lot of sense. My solution will stick the $config['root'] PHP variable into your JS code for you to use.
in mY question 1-Problem is that I can't access the variable from config.php, if it is php I can include it , but how in JS?
You include config.php server side in your PHP code, and then you echo the specific variable inside your javascript, like I show above; Now javascript has your $config['root'] variable.
that javascript is entirely different file hoow will that come to know about config variable?
|
0

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/...)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.