Php/aiax problem

To hide my code in source view, I tried this way: I splitted variabili.inc (with mixed php variable and js) in two files:

file1.inc

<?php
$root=".";
include "$root/definiz_siti.inc";
header('Content-Type: application/json');
echo json_encode([
    'base_url' => $root,
    'base_url_etexts' => $etexts,
    'base_url_fede' => $xmo,
    'base_url_mondo' => $mondooggi,
    'intellectualia' => $intellectualia,
]);
?>

file2.js (same, root, folder)

$.ajax({
    type: 'GET',
    url: 'file1inc',
    dataType: 'json',
    success: function(data) {
        var base_url = data.base_url;
        var base_url_etexts = data.base_url_etexts;
        var base_url_fede = data.base_url_fede;
        var base_url_mondo = data.base_url_mondo;
        var intellectualia = data.intellectualia;

        console.log('Dati ricevuti:', data);

        // Utilizza i valori per caricare i contenuti
        $( "#pelag" ).load(base_url + "/glossario.php #pelagianesimo" ); //in qualsiasi folder
        $( "#gratia" ).load(base_url + "/glossario.php #grazia" ); //in qualsiasi folder
        // etexts di filosofi
        $( "#etexts_aristotele" ).load(base_url_etexts + "/filosofi/antichi/index.php #etexts_aristotele" );
        $( "#etexts_Bruno" ).load(base_url_etexts + "/filosofi/moderni/index.php #etexts_Bruno" );
        // etexts letterari
        $( "#etexts_Virgilio" ).load(base_url_etexts + "/letteratura/0.antica/index.php #etexts_Virgilio"         $( "#etexts_Hopkins" ).load(base_url_etexts + "/letteratura/3.800/index.php #etexts_Hopkins" );
        // etexts religiosi
        $( "#etexts_bibbia" ).load(base_url_etexts + "/religiosi/index.php #etexts_bibbia" );
    }
});

In any footer of php files file2 is called:

<script src=\"$root/file2.js\" defer=\"defer\"></script>";

But this new way doesn’t work (and AI didn’t help :slightly_smiling_face:): no output.
Opening file1.inc in a browser (in local root folder) this is the output:

$root, 'base_url_etexts' => $etexts, 'base_url_fede' => $xmo, 'base_url_mondo' => $mondooggi, 'intellectualia' => $intellectualia, ]); ?> 

EDIT

In php sandbox I get this error

Warning: Undefined array key "SERVER_NAME" in /home/user/scripts/code.php on line 3

line 3 (in “definiz_siti.inc”) is this:

if ($_SERVER['SERVER_NAME'] === "localhost")

Relying on $_SERVER['SERVER_NAME'] is insecure. I’m not sure what you mean by sandbox, but whatever environment you’re running the code in, that setting is not populated.

Looks like your local Webhost has not been configured to parse .inc files with the PHP engine. You should never see dollar signs in output.

Was it supposed to be .inc.php ?

1 Like

no, other .inc work perfectly on my localhost.

For Php sandbox I mean this.

The ‘view source’ is only what was sent when the page was requested. Everything you do using ajax requests on the page can be seen in the DOM by inspecting it in the browser’s developer tools. This is not hiding anything you send to the browser.

The sandbox runs the php code via the CLI (Command Line Interface) and displays the output. This is not making a http request to a web server. Values like $_SERVER[‘SERVER_NAME’] only exist when the php code is executed via a http request on a web server.

Exactly what other .inc files work? If you mean files that you include (you should use require for things your code must have) into other .php files, the php code in the .inc file is being executed in the context of the main .php file, the same as if you copy/pasted the contents of the .inc file where the include statement is at.

If you want php code to “work” in a file that is requested via a http request on a web server, such as an ajax request. simply use a .php extension for that file. By using .inc extensions, anyone can browse to these files and see the raw php code in them.

2 Likes

Yes, but I guess that only few people use inspecting DOM of another website

I see. I will try to change the extension.

EDIT

Nothing changed changing extension from inc to php … :frowning:

Any idea to hide the content that file? If included, it works. But its content is visible…

So, there is no solution?

EDIT

I think that the problem is: how to pass $root variabile within the ajax script.
I mean: if it could be possible to set $root at the beginning of the js, all would work

var base_url = '<?php echo $root; ?>',
$.ajax({
    type: 'GET',
    url: base_url + '/pro-embedded-variabili-generali.php',
    dataType: 'json',

but js doesn’t know the meaning of $root …
that variable is defined in default.inc in every folder, but how could I call that file?
This code, in a (in the) js file, doesn’t work:

<?php include 'default.inc'; ?>
var base_url = '<?php echo $root; ?>',
$.ajax({
    type: 'GET',
    url: base_url + '/myfile.php',

a .js file, unless you specifically set up your server to do so, will not be parsed by the PHP engine.

If it isnt parsed by the PHP engine, PHP code doesnt get translated.

Your PHP file can output javascript. Thats perfectly legal.
Main.html
<script src="thiswilloutputjs.php">

thiswilloutputjs.php

var base_url = '<?php echo $root; ?>',
1 Like

Thank you!
But this doesn’t work:

<?php
include "default.inc";
include "$root/definitions_url.inc";
?>
    <script>
     //to load element from another page
     var base_url = "<?php echo "$root"; ?>";
     var base_url_etexts = "<?php echo "$etexts"; ?>";
     var base_url_fede = "<?php echo "$xmo"; ?>";
     var base_url_mondo = "<?php echo "$mondooggi"; ?>";
     var intellectualia = "<?php echo "$intellectualia"; ?>";

in Chrome console:

Uncaught SyntaxError: Unexpected token '<'

at the beginning of php file

Does the console say its at 1:1 or at 1:5?

1:1, I presume (is only: 1)

Is your current page online somewhere?

1 Like

so far no. Tomorrow I will send a not-linked page to try.
Thank you!

So this error is actually complaining saying the document started with a weird character which is true. If the file itself is a JavaScript intended file, why are you adding in <script>?

I suspect you’re including the file as an external JavaScript file at the same time trying to initiate the file using the inline starting tags. You can’t do that. You either pick 1 way or the other, not both.

1 Like

But nothing changes (deleting <script> tag) with

<?php
include "default.inc";
include "$root/definitions_url.inc";
?>

     //to load element from another page
     var base_url = "<?php echo "$root"; ?>";
     var base_url_etexts = "<?php echo "$etexts"; ?>";
     var base_url_fede = "<?php echo "$xmo"; ?>";
     var base_url_mondo = "<?php echo "$mondooggi"; ?>";
     var intellectualia = "<?php echo "$intellectualia"; ?>";

I have uploaded a file https://www.culturanuova.net/prova-variables.php, as you asked yesterday.
Under “testi completi scaricabili” you should see what you can see here.

okay, that shows me what i suspected.

When you’re including a Javascript file via src, you dont put the <script> tags inside the file. It’s already in a script tag by being in the <script src="..."> one.

Your current code includes a <script> tag.

1 Like

But this is what said also spaceshiptrooper, but it is not this the problem.
I have uploaded a new version without the tag <script> and nothing changed…

It is what spaceshiptrooper said. We both had the same suspicion. More than one troubleshooter can spot the same problem :wink:

It was not “the” problem, but it was a problem. With the script tag there, the included code was not executed.

With it gone, we can now examine the next problem.

Your script does not believe “default.inc” exists, or else default.inc does not include a definition for $root . And from what i can see, i agree with it.

What folder is default.inc in?

1 Like

exactly! You’re perfectly right! :grin: Me stupid! :upside_down_face:
Now I have solved (the other :wink: problem was the name of files called).

Thank you very, very much!

2 Likes