PHP page won't load by JS, with another php file included - javascript

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?

Related

How to include in .php a .html with links to a .js that has php codes

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.

Unable to load js file from php

I have created jquery functions and put them in a js file and called the file from my php using <script src="newajax.js"></script>.
This was working and now it has stopped working.
If i copy all my functions into the php file between the <script></script> tags, It works. I know there is not any issue with the js script.
To test I only added
$(document).ready(function(){
alert("ready");
});
into the js file and called it from my php. It did not show the alert but when I did this inside the php file
<script>
$(document).ready(function(){
alert("ready");
})
</script>
It worked.
So I know there isn't any issue in the php file. It just looks like the script is not being picked up and it was before with exactly the same code. I have not removed the files to a different location. The js files are in the same directory as the php file.
Anybody encountered such an issue? Or know a reason why this could happen.
FYI - I know they are html tags but I have used php extension to be able to run php functions in my file.
The jquery.js file is loaded first before my js file.
When I loaded the webpage in a new tab, it worked. I was refreshing the same browser window when the .js script stopped being called.

i want to include a external php file into html page using javascript?

<?php
header("Content-type: text/javascript");
include("header.php");
?>
<head>
<script type="text/javascript" src="header.php"></script>
</head>
I'm trying to include a PHP (header & footer) file in an HTML file.
The reason it needs to be HTML is this is a content provider that hosts the content and they've put a restriction on the number of characters that can be in the header (as they host it). Any ideas?
You can not include php in a html page, as php generates html but html can not generate php. So you can try ajax method to include php instead on page load try calling a javascript function which will call the php file.
window.location.assign("header.php");
by this method you can add php file but only in php page..not with html.

Load a file with javascript externally using php

I have one file named ad.php. In this file i load some ads from external pages.
(ad.php):
<script type='text/javascript' src='www.example.org'></script>...
And one file named: (index.php).
I want to load the contents of the file ad.php finished and then spend it in the index.php file.
I don't want, that the user loads the javascript files.
i tried some ways:
file_get_contents
curl
But the javascript files were loaded in the index.php by the user.
Is it possible to load the ad.php externally using php?
If you change your php.ini and set
allow_url_include = 1
allow_url_fopen = 1
you can use include("http://example.com/ad.php")
this would be the best way if you don't want to use JS.
If you want to use JS you could use XMLHttpRequests

How do I include external files in PHP?

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.

Categories

Resources