How to embed PHP script in javascript?

I want to know if I can include php

scripts in javascript

[1] in the same way as can be done in html

[2]

server.php:
<?php
    echo 'hello World';
?>

      

client.js (or client.php as needed):

function foo() {
    var i = <?php include('server.php'); ?>
    console.log( i );
}

      

If it can be done, and if it needs to be used instead client.php

, I would really appreciate a minimal example with index.html/index.php

, as I would like to see how I would include client

now when it is a php

and notjavascript

+1


source to share


6 answers


You can use output buffering to achieve something like this, although it is not possible to inject PHP code directly into JS.

Example:

server.php

<?php
    echo 'hello World';
?>

      

client.php - (the .php extension allows you to parse PHP tags but does indeed output 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 only returns a simple string, you can change your code for client.php. Notice how I said "return" instead of output. If your .php server outputs anything, it will be sent to the browser as output (not what you want). Alternatively, you can set a variable in server.php that outputs to client.php or encapsulates your code into 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 can also add a call html_entities

or htmlspecialchars

.

+4


source


It must be a .php file. You should set this as the first line in your client.php:

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

      



Then you can create such a file and just include it and use it as a regular javascript file. So with the tag <script>

as it is now recognized as JavaScript

+2


source


The easiest would be to use client.php instead of client.js, as your server will automatically send the .php file through the PHP interpreter. You can technically reconfigure the server to do this in .js files, but I would not recommend it.

The client.php file must include this line before it outputs anything to the user:

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

      

Regarding the 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.

      

+2


source


To inject PHP into a file, the server has to pass it through a PHP parser, and it will only do so using file types that it said contains PHP markup, i.e. .php files. You could also say that the server parses js files as well, but I'm not sure if the mime type will be set correctly. This will also require changing the server settings. If you are calling a javascript .php file the mime type will definitely be wrong. The most compatible way to do this is 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 simply put the destination variables in the .php file and leave the rest in the 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 the value the PHP variable is set to $SOME_VAR

. Don't forget the quotes when setting up string values. Also, if the PHP code contains bugs, you can infect the javascript code, so a lot of care is needed when doing this.

+2


source


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

// content of myScrpt.php:

<?php 
$fname = "Jason";
$lname = "Kent";
?>

      

  1. Create a file named test.php.

// Content of test.php:

<!DOCTYPE html>
<html>
<head>
<title>Include PHP </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 get:

My full name is Jason Kent.

+1


source


You may not have client side PHP loading. The only way to do this is to have server.php on the server

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

      

Basically loading 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 the variable i can be loaded as a global variable in the page

or as another post suggests, masquerade php file as js.

0


source







All Articles