How to transfer data from PHP to Smarty? - javascript

I have a project that uses Smarty files, but i don't understand how are they parsed from PHP to tpl. I understand that from tpl files to PHP are parsed with js that uses AJAX.
And please share some good AJAX guides, i mostly find people using this nonsense to parse PHP data to javascript
<script type="text/javascript">
var a = <?php echo json_encode($var); ?>;
</script>

To parse data to smarty template I needed to parse the variable in data array or in some modules parse object_data['data'] array

Related

Include PHP variables directly into js files

I would like to use js code, which works with php variables.
My situation now:
mainFile.php
<?
$myPHPvar = 1234;
?>
<html>
<body>
MY CONTENT
<script>
var myJSvar = <? echo $myPHPvar; ?>
</script>
<script src="myFile.js"></script>
</body>
</html>
myFile.js
// Here some js code where I access the myJSvar, which was set by $myPHPvar before
console.log(myJSvar);
This works ! But I would like to change this structure. I would like to move the myJSvar definition out from main file into the jsFile.
But If I move this line var myJSvar = <? echo $myPHPvar; ?> into the myFile.js it will not work, because this is not an php file.
Not I thought that this could be a solution:
I rename the myFile.js to myFile.php and change the code like this:
// Here some js code where I access the myJSvar, which was set by $myPHPvar before
<script>
var myJSvar = <? echo $myPHPvar; ?>
console.log(myJSvar);
</script>
and in the mainFile.php I changed <script src="myFile.js"></script> to include('myFile.php');
This works !
BUT:
First question of all: Is this a good idea to do it like this?
Second: The "problem" now: All my files are now php files, also the files, which includes mainly js code. That's not very beautiful, because now I can't see on the first view, which is an js file and which is a php file.
Thank you for your support !
Using PHP, echo the values to hidden HTML fields on the page.
When you know the page is done loading, read them back out using JavaScript.
Note, Ideally, you'd just make the data a separate AJAX/xhr/fetch call to a URL (JSON or XML formatted data is nice for this), but as a stop-gap, hidden fields will do the trick for basic PHP pages.
Similarly, you can echo some inline JavaScript (tags and all) and reference the variables elsewhere. This approach will work but is often referred to as "spaghetti code" because the intertwined front-end (JavaScript) and back-end (PHP) code makes for tricky debugging and does not scale well to larger code projects... months or years after, developers will find themselves scratching their heads as to where code lives, how it's generated, where new code should be placed, etc.
You can do it like this. And there's nothing really wrong with it. You have to bring the data to the client browser somehow. You can do this with: 1. set php variables in the DOM 2. XHR call or 3. cookies
This can be achieved by generating the JS file dynamically... You can create a PHP file that would generate the JS dynamically like:
Note: THIS METHOD IS HIGHLY NOT RECOMMENDED. THIS IS FOR YOUR INFORMATION AND KNOWLEDGE ONLY.
myFile.php (The JS equivalent file)
console.log(123)
Your HTML file
<script type="text/javascript" src="a.php"></script>
But this is highly not recommended because JS files are static resources that can be cached. Using this method would request the JS file every time the page loads (where it will only be transferred at first load only if its static).
The best method is to keep the dynamic variables in your HTML and then include your static JS files which will use these variables.

PHP to generate JSON for D3

I have a D3 graph which reads a JSON file. Where I am struggling is, what is the mechanism to get the dynamically generated PHP JSON data into the D3 graph.
I think the obvious solution from someone like myself with limited knowledge would be to have PHP write a JSON file to a directory on load, and then have the D3 graph look for it.
However, I feel like there is a better way to do it without writing a json file and reading a json file.
Could anyone please suggest the industry standard method for something like this, so I can continue my reading.
Just request the PHP script from D3 like it is a JSON file. Then in your PHP script...
$data = array('your', 'data', 'here');
header('Content-Type: application/json');
echo json_encode($data);
exit();

Using $_GET in a javascript file parsed with PHP

I have a javascript file I parsed with PHP using
(PARENT)
<script src='js/function.php' type='application/javascript'></script>
and
(FUNCTION.PHP)
header("Content-type: application/javascript");
In this file I need to access query values from the parent document. I have noticed that using $_GET is not returning any values and I'm assuming because its only looking at the function.php file and not it's parent file. Is there any way to access the query values from the parent without passing them down to the function.php file through its own query?
I've done a bit of looking around but can't seem to find an answer.
Any help is appreciated, thank you.

Best way to embedd the PHP variables java in .js files

i have JavaScript files that are included at the top of the page and their content is generated by the php script.
we can echo the php variables easily in the .php extension files but what will be the best solution for .js files?
Either convert the data to json and store that in the file and pass to the script or any other better and efficient approach available?
Use Smarty Template engine. You can access PHP variables in JS file like following:
// Code goes here
PHP file:
<?php
$smarty->assign("Name", "PHP");
?>
HTML file or JS file
<script>
var php_var = {/literal}{$Name}{literal};
</script>

json_encode works in php, but null in javascript

I'm just learning php, and I am trying to parse some files I have, and then send the information to javascript. This is my php:
$datesArray = readFile();
echo json_encode($datesArray);
This echoes:
["120102000000","120102000500","120102001000","120102001500","120102002000","120102002500","120102003000"]
Which is what I want. However, my javascript, which looks like this:
var dates = <?php echo json_encode($datesArray); ?>;
console.log(dates);
keeps giving dates back as null, and I am not sure why. I have tried encoding the array as utf-8, and I have tried using jQuery.parseJSON which also did not work.
Thanks--
Just checking, but is that JavaScript with PHP in a .js file? By default, Apache only runs PHP in files ending in .php so that would mean your PHP is getting parsed as plain text by the server.
If that is the case, the easiest solution is to run that part of the script in some <script> tags.
Alternatively you could make Apache try to serve .js files as PHP file via Mime-Types; but that is really a mess not worth implementing.

Categories

Resources