This code works perfectly, but I can not find how to include an external file.
www.free4g.me/-indexeddb/2.php
Can anyone show how to include a .php file of records?
<?php include_once 'PATH/TO/YOUR/FILE.php' ?>
OR
<?php require_once 'PATH/TO/YOUR/FILE.php' ?>
include = The file CAN be loaded, otherwise it will throw a notice.
require = The file HAS to be loaded, otherwise it will throw an error.
It's not possible include external files, per se.
PHP executes on your server. You'll need to put the www.free4g.me/-indexeddb/2.php file on your server, after which you can include it using the explanation provided by Xatenev.
Related
I have a folder that basically contains:
.php
.html
.js
.css
Inside php i need to load .html to display the webpage of course. inside the html there is a script tag that refers to the .js file.
Now inside the JS file i have a php code that is needed to run there. But using my methods of loading the html the .js throws an error
PHP
<?php
$value = 1;
//$html = file_get_html('index.html')
//include ("index.html")
readfile('index.html');
?>
HTML
<html>
<head>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
Javascript
var myNum = <?php echo json_encode($value); ?>;
Unfortunately the way i have included the html thows an error in the .js file
Uncaught SyntaxError: Unexpected token '<'
What am i doing wrong? Are there any other way to include so that i will be able to write php code in my .js file. Unfortunatly im not allowed to change the file extention there can only be one php file. I separated the javascript and css file to make the code a bit cleaner
EDIT:
A lot may seem to be misunderstanding, This is still hapening in the server, basically what i want is that the webpage recieved by the user already has the value for MyNum. I am initializing the variable before it even gets to the user
In your PHP file, create a global variable containing your JSON in a tag:
<script>var myNum = <?php echo json_encode($value); ?>;</script>
and then reference that variable in your script file with myNum.
PHP code runs on the server, the client (the browser in this case) will get only the output of the PHP. In fact, browsers can't execute PHP code.
JavaScript runs in the client. The browser gets the JavaScript code, and executes it.
The only thing you can do if you really want to produce JS code from PHP is to give a .php ending for the js file (test.js -> test.js.php).
With this, the file will interpreted as PHP. The browser gets the result (javascript, that contains the encoded JSON), and everything works well.
If you want to pass $value from the first PHP to test.js.php, read about GET variables.
while(x<=num_of_cpkgs){
var cpkg_navtray = '\'navtrays/' + cpkg_array[x] + '.html\'';
<?php include ?> cpkg_navtray <?php ; ?>;
x++;
}
cpkg_array contains potentially multiple file names. I'm wondering if there's a way to include a Javascript-generated filename in a PHP include statement like this?
This doesn't work like this.
TL;DR:
You need AJAX calls for this.
Longer:
When you load a page in your Browser, the server send you the file. If the file is a php file, then it calls the php to process the file. After the file is processed the server send it to you as a static file.
With JS you can do some interaction to the website. Until now you probably got used to sending each file as a GET or POST data with a form. With JS you have to make an XMLHttpRequest to create a dynamic request and your page is won't be refreshed again however you will get the response as a variable in JS.
Read all about this here.
In my start.php page I load with JS a file into the header, menu-left and footer div with this code:
$(function(){
$("#header").load("/coach/coach-header.php");
$("#menu-left").load("/coach/team/team-menu-left.php");
$("#footer").load("/coach/coach-footer.html");
});
This works well as long as the loaded php files doesn't have an included php file in itself. When either coach-header.php or team-menu-left.php have:
<?php
include '/php/coach-functions.php';
?>
they don't load, but without it they do.
What can I do when start.php and the loaded .php files all needs to include coach-functions.php and the only place where it works to include it is in start.php, but there I can't get the other php files to get access to the functions and variables in coach-functions.php?
I have website, and I already can highlight code that is written in HTML.
I can store documents on my website, but when I store document (like .php , .c, , .cpp) I can only download them, not opening in next window.
I need some idea how to pass raw code of that file to another page.
I have apache server and I use PHP
I'm not 100% sure what you're trying, but how about posting the file name to a PHP script and reading the file contents using fopen/fread and then just echo it? You should of course make sure that it'll only output files in a specific folder, so this can't be used to hack your site!
<?php
if(isset($_POST['filename']) && is_allowed_to_be_shown($_POST['filename']))
{
$filename = $_POST['filename'];
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
}
?>
Try accessing the pages ending in .php, .c, .cpp, etc via typing in "localhost/[path to file]" in the address bar. That way the files are read by the sever, before being outputted to the browser, meaning that .php (and the like) files will be handled correctly rather than just HTML files.
i am making a "static" php website in this style
<?php include "header.php" ?>
<?php include "left.php" ?>
<?php include "photos.php" ?>
In "photos.php" i have 3 heavy javascript files from the lightbox and i thought it could be a good to include the javascript files only in this "photos.php" and not at the "header.php".
But javascript supposed to be only in the head html tags. Do you have any better approach or mine is just fine?
thanks very much
It's best to have all javascript in the head whenever you can. And you can without much difficulty. As Dominic Rodger said, it's probably not a big deal to include the js files on every page because they should be cached.
I tend to create page template class files with lots of variables for this sort of thing. A simpler thing to do that's more inline with what you're already doing is to set a variable before you include the header file, then access that variable in the header file and add the js if appropriate.
<?php
$includePhotoJavascript = true;
include "header.php";
?>
In the header file:
if(isset($includePhotoJavascript) and $includePhotoJavascript == true)
{
// add the javascript
}
JavaScript does not only have to be in the head HTML tag. It is actually advisable to put it at the end of the HTML file, because they halt the rest of the HTML file from loading.
What you could do in the header.php file, is something like this:
<html>
<head>
<title><?php print $title; ?></title>
<?php
foreach($javascript as $src){
?>
<script type="text/javascript" src="<?php print $src; ?>"></script>
<?php
}
?>
</head>
Then the file you posted would look like this:
<?php
$title = "Photo album";
$javascript = array("jsfile1.js", "file2.js");
include "header.php"
include "left.php"
include "photos.php"
?>
If you're going down this route, you could try setting a variable before you include header.php which stores whether or not those JavaScript files are needed. It might not be necessary to sweat it too much though, if users tend to stick around on your website they'll fetch those files once and then not again, since hopefully your server will return a 304 Not-Modified response, and they'll be served from your browser's cache.
thanks very much for your ideas.
i am planning of making the home page(index.php) as light as posible. So
i will load javascript(lightbox.js) only in photos.php and try to do a
LAZY LOADING to this lightbox.js so when "first time" visitor go to
photos.php javascript file will be in cache.
and all these in background.
I think is the same Facebook does. Look here
Optimizing Facebook