JavaScript: load file that contain include PHP fucntion in div - javascript

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()

Related

How to write JS function in a .js file

I wrote a function in Html and it works well.
But My teacher said we need separate the code out of HTML file. So I need to implement this code in a .js file. Can anyone tell me how to do that? I think to create a function in JS like this but it not working.
Thanks for any help!
<script>
$(document).ready(function() {
$("#down").on('click',function(event) {
$('html,div-b').animate({scrollTop: document.body.scrollHeight
-1100},"slow");
});
});
You have use html file like this
<html>
<body>
<script src="demo.js">
</script>
</body>
</html>
and keep the js file like this
demo.js file
function test(){
<!---your code here-->
}
copy the code inside the <'script> tag and paste in a separate .js file . This is how it works
Put all the js code in a .js file, then put the code below in the html page which will call it, inside the head or the body.
<script src="myScript.js"></script>
Write the code as it is in file and save it with .js extension and link it in html under head tag as follows
<script src="myscripts.js"></script>

Substituting PHP into 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!

Include code in html file in captcha context

I've a file that needs to have a php include
<? include_once("script.php") ?>
Javascript content
<script>
Some javascript code
</script>
And HTML content:
<some tags>
So that I just have to do code to include in html and everything works as I need in the captcha.
I've tried <link>, <iframe seamless> and <script> but none worked... I need this piece of code to interact with the parent code, just like a regular include... How could I do this?

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