Substituting PHP into JavaScript - javascript

I notice that I can substitue PHP into JavaScript that's place inline in the HTML, as below:
<script>
jQuery('select#age_group').val(<?php echo $age_group?>);
</script>
But the PHP substitution doesn't work inside a .js file that the PHP file loads. Why is that and how can I do the substituion in the loaded .js file?

Here are two ways you can do this without changing the file extensions that the PHP engine can process.
First, you can set a global variable inline and then access that variable in the JS you include.
<script>global_age_group = <?php echo $age_group?>;</script>
<script src="js_that_accesses_age_group.js" /></script>
(standard warnings about global variables apply)
Second, you can give your JS file a .php extension, so...
<script src="js_that_accesses_age_group.php" type ='application/javascript'/></script>
And then use
<?php echo $age_group?>
in that file as you do in your inline example.

Try this instead... Let me know if it works
</form>
</div>
<?php echo"<script>
jQuery('select#age_group').val($age_group);
</script>";
?>
</body>
</html>

you pass the value on a element attribute or on js var as you did
<script>
var age = 20;
</script>
<script src="main.js"></script>
and your can use the age inside main.js

By default, PHP will only process files ending in certain extensions (most commonly .php). If you want to use PHP in a JavaScript file then the easiest way to do it is to give it a .php extension.
Most browsers will also refuse to process your JavaScript unless it has the right content type, so at the top of your file add
<?php
header("Content-Type: application/javascript");

The Simple answer that you can't !
You can't write a PHP script any where except .php files,
That you can't write php scripts in .html, .css or .js files because the compiler will not pass .js file or .html file to read it!

Related

How to include inline JS files with PHP?

Normally that is how one includes JS files in HTML:
<script type="text/javascript" src="myFile.js" ></script>
But I want to write the JS file inline the HTML, using PHP; but still having the possibility of having my neat JS file, that can be chached, debugged and minified.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
In other words, how to include a JS file in HTML on the server-side. That may get a lot of advantages because it is transparent to the browser.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
Exactly that code would work, yes. Though it would try to interpret the file as PHP code, and if anything looked like <?php .. ?> inside that file you may see weird side effects. If you want to include the file uninterpreted, simply do:
<script><?php echo file_get_contents('myFile.js'); ?></script>
Be aware of possibly having to escape "</script>" now, should that appear in your included file; see https://stackoverflow.com/a/6908901/476.
YOu need to make custom function or use :
echo '<script type="text/javascript" src="myFile.js" ></script>';
Or
function add_js($filename){
return '<script type="text/javascript" src="'.$filename.'" ></script>';
}
echo add_js('myFile.js');

jquery script tag visible in each page

I have included this code using php "include" at the end of the body tag in
</div>
<?php include "jq.php" ?>
</body>
</html>
contents of jq.php----------------------------------------------------------------------------------
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
if (!window.jQuery)
{
document.write('<script src="./script/jquery.min.js"><\/script>');
}
</script>
-----------------but it is visible at the end of each page----------------------------
There's still not much to go on why this isn't working without looking at a lot more of your codebase.
I'd suggest a few things. Firstly, that you probably don't need to include a local copy unless you're doing development work - the chances of Google's CDN being down are very low, and if the user can't access it you'll likely have trouble with other components of the page anyway.
If you do need the local version for development (e.g. if you want to test the site when you're offline) then simply add this to PHP:
if (is_development()){
echo '<script src="./script/jquery.min.js"></script>';
}
else {
echo '<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>';
}
Where is_development() checks some defined CONSTANT or (less ideally) a global variable or local file.
The final approach would be to simply include the jQuery code at the top of your own JS file:
if (!window.jQuery){
var jQuery=.... // full minified jQuery here
}
None of those really answer your query but they should help you continue with your development without too many more problems relating to this issue.

JavaScript: load file that contain include PHP fucntion in div

I have this situation.
file.html
.......
<div id="mydiv">
</div>
.......
myjsfile.js
............
$("#mydiv").load("myphpfile.html");
............
myphpfile.html
............
<?php
include("myfile.php");
?>
............
The content of myphpfile.php is not viewed. How can I show the content of myphpfile.php in selected div?
Thanks.
You can use php also in load funtion.
$(document).ready(function(e) {
$("#mydiv").load("myfile.php");
});
Above code will load after page loaded
include("myfile.php");
include function will run before loading html and javascript codes..
Use this script file for library jquery-1.9.1.js
We could not able to include PHP file with in HTML file. You have to call php file instead of HTML file.
In PHP, there are many function to include Myfile.php file. please find the include function as below:
include()
require()
include_once()
require_once()

PHP require_once() .js file

Okay, so I'm using require_once to load the contents of a .js file. However, there seems to be a 1 added to the end of the file after the require is done.
PHP:
echo require_once("test.js");
JS:
var newFunc = function() {
console.log('yay!');
};
rendered html when the PHP file is loaded:
var newFunc = function() {
console.log('yay!');
};1
require_once() returns 1 if the include was successful. So you're echoing the return value, which is not what you thought it was.
Use file_get_contents() instead, although this is odd usage, you're probably heading in the wrong direction with whatever you're trying to do with this...
If you want to import a script "on the fly" and want it to be only loaded once, we can "combine" require_once with a script, like so:
<script>
<?php require_once('your-script.js'); ?>
</script>
<div>Foo</div>
Now, let's you have a component with that structure. If some of your templates includes that component many times, your script will only be embedded once, in the first one.
You should remove echo from "echo require_once("test.js");"
you can include the js file by following method
<script src="test.js" language="javascript"> </script>

Is it possible to put a javascript function in a php file?

Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>

Categories

Resources