How to embed php script in javascript?

前端 未结 6 848
余生分开走
余生分开走 2020-12-11 12:26

I want to know if I can include php scripts in javascript[1] the same way it can be done in html[2]

server.php:


        
相关标签:
6条回答
  • 2020-12-11 12:40

    You can use output buffering to achieve something similar, although it's not possible to directly embed PHP code in JS.

    Example:

    server.php

    <?php
        echo 'hello World';
    ?>
    

    client.php - (.php extension enables the ability to parse PHP tags, but really outputs JS)

    <?php
    header("Content-type: application/x-javascript"); // Or 'text/javascript'
    ob_start();
    include( 'server.php');
    $i = ob_get_contents();
    ob_end_clean();
    ?>
    function foo() {
        var i = '<?= json_encode( $i); ?>';
        console.log( i );
    }
    

    Edit: If the server.php file will only return a simple string, then you can modify your code for client.php. Notice how I said "return" instead of output - If your server.php outputs anything, it will be sent to the browser as output (not what you want). Alternatively, you can set a variable in server.php which gets outputted in client.php, or encapsulate your code in a function:

    server.php

    <?php
    function js_output()
    {
        return 'hello world';
    }
    ?>
    

    client.php

    <?php
    header("Content-type: application/x-javascript"); // Or 'text/javascript'
    ?>
    function foo() {
        var i = '<?php include( 'server.php'); echo addslashes( js_output()); ?>';
        console.log( i );
    }
    

    Note: You may also want to add a call to html_entities or htmlspecialchars.

    0 讨论(0)
  • 2020-12-11 12:40

    I tested this solution on PHP 5.5.
    1. Create a file called myScript.php.

    // myScrpt.php content:

    <?php 
    $fname = "Jason";
    $lname = "Kent";
    ?>
    
    1. Create a file called test.php.

    // test.php content:

    <!DOCTYPE html>
    <html>
    <head>
    <title>Include php Script</title>
    
    <script type="text/javascript">
    <?php include 'myScript.php'; ?>
    </script>
    
    </head>
    

    <h1>The result is:</h1>
    <?php
    echo "My full name is $fname $lname.";
    ?>
    
    </body>
    </html> 
    
    1. When you open test.php in a browser, you will get:

    My full name is Jason Kent.

    0 讨论(0)
  • 2020-12-11 12:43

    It has to be a .php file. You need to set this as the first line in your client.php:

    header("Content-type: text/javascript");
    

    Then, you could create the file like that, and just include it, and use it as a regular javascript file. So with the <script> tag, as it is now recognized as JavaScript

    0 讨论(0)
  • 2020-12-11 12:50

    To embed PHP in a file the server needs to pass it through the PHP parser and it will only do that with files-types it has been told contain PHP markup, i.e. .php files. You could potentially tell the server to parse js-files as well, but I'm not sure if the mime-type would be set correctly. It would also require you to change the server settings. If you call the javascript-file .php the mime-type will definitely be wrong. The most compatible way to do this would be to put the javascript in the html-file (i.e. your .php file). If you want to keep the amount of javascript in your .php files to a minimum you can just put the variable-assignments in the .php file and keep the rest in your javascript file.

    server.php:

    <script language="javascript">
        var some_var = "<?php echo $SOME_VAR ?>";
    </script>
    
    <script src="client.js" type="text/javascript">
    

    client.js:

    alert(some_var);
    

    The (global) javascript variable some_var will be set to whatever the PHP variable $SOME_VAR is set to. Don't forget the quotes when setting string values. Also if the PHP code contains errors you could contaminate the javascript code so alot of care has to be taken when doing this.

    0 讨论(0)
  • 2020-12-11 12:51

    You cannot have a client load PHP. The only way you can do this is to have on server.php

    <html>
        <head>
              <script type="text/javascript">
              function foo() {
                 var i = <?php echo 'hello World'; ?>
                  console.log( i );
               }
              </script>
        </head>
        <body>
        </body>
    </html>
    

    Basically loading the javascript into the body of the page. Or alternatively.

     <html>
        <head>
              <script type="text/javascript">
                 var i = <?php echo 'hello World'; ?>
              </script>
              <script type="text/javascript" src="client.js">
        </head>
        <body>
        </body>
    </html>
    

    And in client.js

    function foo() {
       console.log( i );
    }
    

    essentially variable i can be loaded as a global variable on the page

    or as another post suggest, masquerade a php file as a js.

    0 讨论(0)
  • 2020-12-11 12:54

    The easiest thing to do would be to use client.php instead of client.js, since your server will automatically send a .php file through the PHP interpreter. You technically could reconfigure the server to do it to .js files, but I wouldn't recommend it.

    The client.php file should include this line before anything is outputted to the user:

    header("Content-type: text/javascript");
    

    As for a basic index.html/index.php example:

    <html>
    <head>
    <title>Example</title>
    <script type="text/javascript" src="client.php"></script>
    </head>
    <body>
    ...
    </body>
    </html>
    

    Update: example client.php file:

    <?php
        header("Content-type: text/javascript"); // This bit must come first!
    ?>
    var i = "<?php echo $somevar; ?>" // This is JavaScript code with some PHP embedded.
    
    0 讨论(0)
提交回复
热议问题