I have two php files. The one is the main file and the second file Im including like this:
— main.php —
include('secondfile.php);
— secondfile.php —
The layout of this file looks like this. I am trying to insert an AJAX function.
<?php do check for cookie ?>
<script>
myfunction()
console.log('some text to see if this works');
myfunction(){
insert AJAX call
}
</script>
Both main and secondfile.php do not include an HTML layout with head and body. Why is the code in the second file not working? Not even the console log test shows.
thank you but I think there is more than just where I declare the function. I just moved it to the bottom and still no success. Also my console message is not displaying.
Actually no, nevermind. Even if it is, PHP is going to reject it.
include basically says "At this point, take what’s in that file, and paste it into my PHP file.
Your secondary file is a piece of Javascript. So PHP is going to bork as soon as it gets to the <script>, and say “That’s not PHP code.”
If you’re trying to OUTPUT the second file, echo file_get_contents(...) it instead.
INclude brings PHP code IN to the current script.
To output something to the browser, you need an output function (echo. No, I couldnt shoehorn ‘out’ into it somewhere. shh).
yes, that should work. (For the record, it doesnt need to be a .php file. Personal style at that point, but i’d call the file admin_check_access.js, because it’s a Javascript file.)
hmmm - this is still not working. Could it be that I am using the following chain of files using ajax inside php.
Dashboard — making an Ajax call to get_chart1 - which Im trying to make a call to — admin_check_access.php
The reason why is I have many API’s like get_chart1 and I am busy testing for a JWT user token at the start of those API’s for added security to my site. So all of my php API’s need to have this check.