Jump to content

php variable in external js file


Destramic

Recommended Posts

when tyring to pass a variable to a extenal javascript file it just shows up as a sting and the variable isnt beening executed

var timezone_offset = <?= $timezone_offset ?>;

im guessing its probably down to the fact the variable is being passed from my framework controller...

so im wondering how do you send a variable to a external javascript file when using a framework?

$this->view->timezone_offset = "test";   // in my controller

// passing varibales to template in my view	

if (preg_match("/\.html$/i", $file_name))
{
	extract($this->_variables);
	require_once PRIVATE_DIRECTORY . 'application' . DS . 'views' . DS . $file_name;
}

thanks guys

Link to comment
https://forums.phpfreaks.com/topic/292168-php-variable-in-external-js-file/
Share on other sites

well the php variable is in the extental js file which is included in the head...is this what your asking for?

<!DOCTYPE html> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8" />
<script src="http://127.0.0.1/bisi/media/js/library/jquery/2.1.1/jquery.min.js"></script>
<script src="http://127.0.0.1/bisi/media/js/library/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script src="http://127.0.0.1/bisi/media/js/scripts/global.js"></script>   // here is the line of the file where php var is in
<script src="http://127.0.0.1/bisi/media/js/scripts/user_timezone.js"></script>
<link rel="stylesheet" type="text/css" href="http://127.0.0.1/bisi/media/css/items/items.css" />
<title>BiSi</title>
</head>

well i did a simple test and still did get the variable passed accross...but didnt work...i dunno what im doing wrong

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php $timezone_offset = "test"; ?>
<script>
var time = "test";
</script>
<script src="global.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</body>
</html>

global.js

alert(time);
var timezone_offset = "<?php echo $timezone_offset; ?>";
alert(timezone_offset);

Ajax method

 

Test3.js

var testvar = 'Failure';

function checkVar() 
{
    alert(testvar);
}

Test1.html

<html>
<head>
<script type="text/javascript" src="test3.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
    $().ready(function() {
        $("#btnTest").click(function() {
            checkVar();
        })
        
        $.get(
            "test2.php", 
            function(data){
                testvar = data;
            },
            "text"
        )
    })
</script>
</head>
<body>

<input type="button" name="btnTest" id="btnTest" value="Test Var">

</body>
</html>

Test2.php

<?php
    echo "Success";
?>

Assuming the PHP Script is run on the same page you're loading, and above the actual <script> import tag, you could make it easy on yourself.

 

A lot of applications use their own global object to keep some form of data that is fairly unionized throughout more than one page. A way you can do this is something like this:

<script>
MyApplication = { meta: {} }; //Global object MyApplication. For this example, with 'meta'
MyApplication.meta.MyVariable = '<?php echo $timezone_offset;?>';
</script>
<script src="path/myJsFile.js"></script>

And then in your external JS file you do

var myVariable = MyApplication.meta.MyVariable;

And you have access to it. http://i.imgur.com/VKUBfLm.png

 

You can also do this if you have an array of items you want accessible in the frontend, by doing something like this..

<script>
MyApplication = { meta: {} };
<?php
foreach($myList as $key => $value) {
echo "MyApplication.meta." . $key . " = '".$value."';";
}
?>
</script>

Hope it helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.