admin管理员组

文章数量:1289636

when I enter in code like this:

<p>Hello <? echo $name; ?>, How are you?</p>

It prints:

<p>Hello <!--? echo $name; ?-->, How are you?</p>

As a ment. I have it in a file called base.js with this code:

function showName() {
   document.getElementById("name").innerHTML = "<p>Hello <? echo $name; ?>, How are you?</p>";
}

So I embed the .js file like so:

<script type="text/javascript" src="base.js"></script>

So, after it changes the <p id="name"></p> I get:

<p id="name">Hello <!--? echo $name; ?-->, How are you?</p>

I had the code in the .php file, and it seemed to work fine. Now that I have it in a separate base.js file, it ceases to function. Help!

when I enter in code like this:

<p>Hello <? echo $name; ?>, How are you?</p>

It prints:

<p>Hello <!--? echo $name; ?-->, How are you?</p>

As a ment. I have it in a file called base.js with this code:

function showName() {
   document.getElementById("name").innerHTML = "<p>Hello <? echo $name; ?>, How are you?</p>";
}

So I embed the .js file like so:

<script type="text/javascript" src="base.js"></script>

So, after it changes the <p id="name"></p> I get:

<p id="name">Hello <!--? echo $name; ?-->, How are you?</p>

I had the code in the .php file, and it seemed to work fine. Now that I have it in a separate base.js file, it ceases to function. Help!

Share Improve this question asked Feb 20, 2012 at 21:42 SnarkyDThemanSnarkyDTheman 2654 gold badges7 silver badges17 bronze badges 5
  • That is because it is no longer php. – mplungjan Commented Feb 20, 2012 at 21:43
  • 2 You can not evaluate PHP code from Javascript! Ok if you tell the server to preprocess .js files as PHP you can get away with that; try calling the file .js.php – The Nail Commented Feb 20, 2012 at 21:44
  • PHP has to be parsed before you output javascript. You probably need <?php or you need to change your webserver configuration. Putting PHP in a JS file won't work at all because the webserver won't know to parse it through PHP. PHP is server side. Javascript is client side. – Cfreak Commented Feb 20, 2012 at 21:45
  • apparently *.js files get not parsed by php on your server. you can in fact parse .js-files if you want: encodable./parse_html_files_as_php But i would not remend... – zaphod1984 Commented Feb 20, 2012 at 21:45
  • 1 that's right. apache determines whether to interpret PHP-code in a file from the file name's extension, usually *.php. you can just rename the file base.js.php and include it the usual way. your browser won't mind. <script type="text/javascript" src="base.js.php"></script> – Basti Commented Feb 20, 2012 at 21:46
Add a ment  | 

4 Answers 4

Reset to default 6

That is because it is no longer php.

Change to

<script type="text/javascript" src="base.php"></script>

and have a 
<?php header("content-type:text/javascript"); 
$name = "...";
?>

function showName() {
   document.getElementById("name").innerHTML = "<p>Hello <?php echo $name; ?>, How are you?</p>";
}

Or

change to

function showName(name) {
   document.getElementById("name").innerHTML = "<p>Hello "+name+", How are you?</p>";
}

and in the php file have

<script>
// using json_encode to make the string safe for script injection. 
// Still needs quotes for a single string
showName("<?php echo json_encode($name); ?>");
</script>

PHP is processed on the server side, not client side. You cannot execute PHP code on the client side.

Thist of all you cannot run php in the JS. The second is you have to do smth like this to avoid problem with ments:

//<![CDATA[
    function showName() {
       document.getElementById("name").innerHTML = "<p>Hello <? echo $name; ?>, How are you?</p>";
    }
//]]>

speaking from 2021 :

1- you can use js and php.

2- CDATA is deprecated.

本文标签: javascriptPHP Code gets turned into HTML lt Comments gtStack Overflow