Dibish
Dibish

Reputation: 9293

Accessing Apache environment variables from javascript

I have a requirement to access apache environment variable from js file. I am not sure this is possible or not.

I used to set Apache variables and access it in php using the following way

Set ENV varialbe

SetEnv PAYPAL_MODE live

From php

echo $_SERVER["PAYPAL_MODE"]; 

My question is can i access "PAYPAL_MODE" in my js file

<html>
  <head>
    alert(PAYPAL_MODE);
  </head>
  <body>

  </body>
</html>

Upvotes: 4

Views: 3037

Answers (3)

Ra&#250;l Mart&#237;n
Ra&#250;l Mart&#237;n

Reputation: 4679

You can do this:

var paypal_mode = "<?= $_SERVER["PAYPAL_MODE"];?>";
alert(paypal_mode);

You need to be sure that you want expose that variable

Upvotes: 2

Alexandre Nucera
Alexandre Nucera

Reputation: 2223

Well, you could do something like this in your html :

<script>
$(document).ready(function() {
    window.paypal_mode = <?php echo json_encode($_SERVER["PAYPAL_MODE"]); ?>;
});
</script>

And access it through the window object in your javascript file.

Upvotes: 0

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

Try this,

<head>
    <script>
        alert("<?= $_SERVER["PAYPAL_MODE"];?>");
    </script>
</head>

Upvotes: 0

Related Questions