-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…

6
  • 1
    Explanation? Learn about when PHP runs. Mar 5, 2016 at 1:18
  • php runs on this page. so output1 is generated.
    – ratmalwer
    Mar 5, 2016 at 1:22
  • php isn't compiled in js files ... it runs in php files
    – charlietfl
    Mar 5, 2016 at 1: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 5, 2016 at 1: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. Mar 5, 2016 at 2:14

1 Answer 1

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

4
  • i changed my function-call to php_test('<?php echo(DIR); ?>'); I think that performs better now I know more about that subject.
    – ratmalwer
    Mar 5, 2016 at 2: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 5, 2016 at 3: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 5, 2016 at 3:11
  • nice piece of work. I think I got the idea of how it works.
    – ratmalwer
    Mar 5, 2016 at 3:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.