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>
You can do this:
var paypal_mode = "<?= $_SERVER["PAYPAL_MODE"];?>";
alert(paypal_mode);
You need to be sure that you want expose that variable
Try this,
<head>
<script>
alert("<?= $_SERVER["PAYPAL_MODE"];?>");
</script>
</head>
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.
Related
I have three php files, one defines a constant, the other has the html markup and includes a php file wigh renders js content. the third is the php file rendering the js content. the code is as shown below.
the first file named "config.php" :
<?php
define('TESTVAR','test variable');
?>
the second file named "main.php" :
<?php
require_once ('config.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script type="text/javascript" src="script.php"></script>
</body>
</html>
the third file named "script.php" :
alert(<?php echo TESTVAR ?>);
this is what gets loaded as script.php:
<b>Notice</b>: Use of undefined constant TESTVAR - assumed 'TESTVAR' in <b>C:\xampp\htdocs\trials\testphpinjs\script.php</b> on line <b>2</b><br />
TESTVAR);
it's not that php is not being rendered, if i change the script.php to
alert(<?php echo '\'testing'. 'php' . 'echo\'' ?>);
it works perfectly and the loaded script can be viewed from developer tools as
alert('testingphpecho');
the only problem I have is that I cannot access the variables in pages required or included before
Using <script type="text/javascript" src="script.php"></script>, the browser will issue a new request to load script.php directly. Since PHP is stateless, that request will not be handled in the same context as whatever processing goes on in main.php.
If you want to use the variables/constants defined in config.php within script.php, you will have to load it in there:
<?php require_once('config.php'); ?>
alert("<?php echo TESTVAR; ?>");
Note that I've added quotation marks, since TEST_VAR is a string value in your example.
If your Javascript code relies on more than just config.php and its behavior depends on pre-processing that's done in main.php, it might be better to load it as an inline script instead:
<?php
require_once ('config.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<script type="text/javascript">
<?php include('script.php'); ?>
</script>
</body>
</html>
This way, PHP is loading the script.php file within the same context as main.php, and all constants and variables defined there will (should) be available for use by the code that's outputting your Javascript.
I need to create a centralized page where all the variables that are needed in more than one page get set. For example:
centralized.php
$displayPath = "/var/www/html/wordpress/";
Then, I need html and php pages across the server to have access to it. For example:
page1.html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
</body>
</html>
<?php
echo ($displayPath);
?>
page2.php
<?php
echo ($displayPath);
?>
I was looking at using window.localStorage, but when doing so, how could pass the value from JS variables to php variables.
If you only want to read the variables from different pages, you can create a file with all of your variables:
myVariables.php
<?php
$displayPath = "/var/www/html/wordpress/";
$myglobalvariable="hello";
?>
And then include it in every page that needs it.
page1.php
<?php
require_once("./myVariables.php");
?>
NOTE: This will not allow you to change the variables in one page and then access the modified variables in another page. If that's what you want, you can take a look at this:
How to access a variable across two files
Is it possible to use coffeescript inside .php file, for example :
<!--test.php-->
<html>
<head>
<title> use coffeescript inside php file
</head>
<body>
<h1>how to use coffeescript</h1>
<p><?php echo $lorem ?></p>
<script type="text/javascript">
// i want to use coffeescript here
(function() {
alert ('hello world')
});
</script>
</body>
</html>
If I want to use coffeescript in my php file what should I do to make it work?
I see no reason why not.
https://github.com/alxlit/coffeescript-php
Load your coffee in a var, compile it, and stick the resulting JS variable between the <script> tags.
I wanted to access a session variable in javascript in asp.net mvc application. I have found a way to do it in aspx view engine but not in razor.
Please tell me a way to access the session variables
You can do it this way for a String variable:
<script type="text/javascript">
var someSessionVariable = '#Session["SomeSessionVariable"]';
</script>
Or like this if it's numeric:
<script type="text/javascript">
var someSessionVariable = #Session["SomeSessionVariable"];
</script>
This is really not a very clean approach though, and requires inline JavaScript rather than using script files. Be careful not to get carried away with this.
I personally like the data attribute pattern.
In your Razor code:
<div id="myDiv" data-value="#Request.RequestContext.HttpContext.Session["someKey"]"></div>
In your javascript:
var value = $("#myDiv").data('value');
In my asp.net I am not getting the result by
<script type="text/javascript">
var someSessionVariable = '#Session["SomeSessionVariable"]';
</script>
But I get the answer by below code,
<script type="text/javascript">
var yourVariable = '<%= Session["SessionKey"] %>';
</script>
For google searchers,
In addition, If you want to access the session variable in external .js file you can simply do like this,
------ SOME HTML PAGE ------
//Scripts below Html page
<script>
//Variable you want to access
var mySessionVariable = '#Session["mySessionVariable"]';
</script>
// Load External Javascript file
<script src="~/scripts/MyScripts/NewFile.js"></script>
Inside NewFile.js
$(document).ready(function () {
alert(mySessionVariable);
});
I have a .json file in a bucket on S3. I'm trying to parse information from the file, a date and a SigninSim. I am doing this through an html file which once I get this figured out will take that parsed information, go into another folder, and display some pictures. Here is the code that I currently have written.
<!DOCTYPE html>
<html>
<head>
<script src="https://s3.amazonaws.com/'BUCKET'/browser.json"></script>
<script type="text/javascript">
function parseJSON()
{
var info = JSON.parse(browser);
document.write(info.date);
document.write(info.SigninSim);
}
</script>
</head>
<body>
<script type="text/javascript">
parseJSON();
</script>
</body>
</html>
When I run this nothing shows up on the page. Any ideas? I'm also very new to html/javascript so I could be doing something completely wrong, anything helps!
<script> tags can only be used to execute Javascript code, not to read JSON files.
Change the JSON file to a Javascript file that creates global variables or objects.