-1

I have the following script that works perfect.

<script type="text/javascript">
$(document).ready(function() {
        php_test();
});
</script>     

<script type="text/javascript">
function php_test() {
    alert('<?php echo(DIR); ?>myfile');
}   
</script>

The output is as expected:

http://localhost/mvc_framework/myfile

when I put the function php_test in a file lets say ‘php_test.js’ and bind it to my footer it executes with this output:

<?php echo(DIR); ?>myfile

Any explanation? Im confused…

ratmalwer
  • 700
  • 5
  • 14
  • 1
    Explanation? Learn about when PHP runs. – John Dvorak Mar 05 '16 at 01:18
  • php runs on this page. so output1 is generated. – ratmalwer Mar 05 '16 at 01:22
  • php isn't compiled in js files ... it runs in php files – charlietfl Mar 05 '16 at 01:41
  • This isn't a bad question to ask to be honest, just the way you worded it makes it confusing and people are downvoting. – Aaron Mar 05 '16 at 01:44
  • 1
    @Aaron It's a poorly researched one. Knowing the relation between PHP and Javascript is a prerequisite for programming in either (and if the asker does know, he didn't do the best job telling us). It's also a duplicate big time. – John Dvorak Mar 05 '16 at 02:14
  • @JanDvorak definitely agree about being a duplicate. Some users have a hard time figuring out what keywords to base their research on. Especially users who are still grasping the languages they are trying to learn. My solution to this was, after-all, found on stackoverflow after a quick search for the words that mattered haha, I don't remember htaccess stuff off by heart :P – Aaron Mar 05 '16 at 03:20

1 Answers1

1

The way you asked the question makes it confusing. It is possible to make PHP run on all types of files on your server with a bit of Apache tweaking. My solution will make your JS files be processed by the PHP interpreter.

What you need to do is create a .htaccess file if you are using Apache. I am going to assume you are. Then you add this line into it:

AddType application/x-httpd-php .php .js

The above code will force the PHP interpreter to run on all the formats listed in the command. You can also add .htm or even .css if you need PHP to do something with those files on the server side.

Refer to this question here for a previous solution to similar question > Using .htaccess to make all .html pages to run as .php files?


Or you can just store a whole bunch of variables from the PHP end on the page as Javascript variables like this example from one of my projects:

        <script type="text/javascript">
            var trackFilterFlag = null;
            <?php
                echo "trackFilterFlag = \"". $displayedPageType ."\";\r\n";
            ?>

            var trackFilterCategory = null;
            <?php 

                if(strcmp($displayedPageType, "mood") === 0 || strcmp($displayedPageType, "genre") === 0) {
                    echo "trackFilterCategory = \"". $filterCategory ."\";\r\n";
                } 

            ?>

            var sortingTracksBy = null;
            <?php
                if( isset($chosenSortFlag) && strlen($chosenSortFlag) > 3 && !($defaultSort) ) {
                    echo "sortingTracksBy = \"". $chosenSortFlag ."\";\r\n";
                }
            ?>

        </script>

Of course I was still a novice when I wrote that code, it's possible to make it much neater and just make PHP echo the whole thing, but you understand what I mean :)

Community
  • 1
  • 1
Aaron
  • 394
  • 2
  • 8
  • i changed my function-call to php_test(''); I think that performs better now I know more about that subject. – ratmalwer Mar 05 '16 at 02:19
  • Ahhh that works. Well, theoretically yes it will slow down performance a tiny bit, but it is negligible to be honest. Imagine that your single .php page was loading 5 JS files, then yes, the PHP pre-compiler/interpreter will be run 6 times instead of just 1 time for the .php file only. But, in the grand scheme of things, this does not severely bring down performance, this is a legitimate use of the HTACCESS file and PHP developers already know that there are plenty of websites out there which have dynamically created .js and .css files for all kinds of purposes :) – Aaron Mar 05 '16 at 03:08
  • Another thing you could do also is to set a whole bunch of global variables in the Javascript area. I'll add an example code of that to the bottom of my answer, but, sounds like you are already on top of it :) – Aaron Mar 05 '16 at 03:11
  • nice piece of work. I think I got the idea of how it works. – ratmalwer Mar 05 '16 at 03:20