Insert ajax into php

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.

Because you declare your function after call. JS reads and executes its code row by row.

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.

Well if it looks like that, PHP is going to throw a hissy fit because that’s not legal PHP code.

trust me it doesnt look like that LOL

So show us what it DOES look like. Because you’ve done something that’s caused it not to work, and we need to see it to fix it.


Above you will see the two files. The INCLUDE is in line 10.

the include is pointing UP a directory. Is the second file a directory above your main one?

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.

The include file is on the same level as the file containing it…

domain.com/admin/get_chart1_data.php
domain.com/jwt/admin_check_access.php ----------- this is the include

ok I will try that thanks. Yes even if I dont use the include and paste the code inside the first file it still wont work

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

Is this how I do it?

echo file_get_contents(‘…/jwt/admin_check_access.php’);

Sorry im still learning this stuff :frowning:

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.

Right so, when you view the resulting page’s code by Inspecting it, do you see the javascript in the HTML?

Ok when I view the source I do see the javascript

Ohhh I apologise. I didnt see the PHP line in the javascript. We DO need to include the file, because it contains PHP code that needs to be parsed.

Put the include back in, and check your source again. Is the username being set appropriately?