0

I have an PHP file that I want to pass a parameter from it to a JavaScript file , it seems simple but I just can't execute it , my way doesn't work and I don't know why!!

These are the codes of the 2 files plus another HTML code to view the result ,,`

the php file ..

<?php
$ser = "server";
?>
<script type="text/javascript">var server = "<?= $ser ?>";</script>
<script type="text/javascript" src="new4.js"></script>

new4.js (the Java script file)

function myFunction() {

alert("server: " + server);

}

And this's the HTML file to view the result

<!DOCTYPE html>
<html>
<body>
<script src="new4.js"></script>
<p>Click the button to demonstrate line-breaks in a popup box.</p>

<button onclick="myFunction()">Try it</button>

</body>
</html>
Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
Essam wageh
  • 39
  • 1
  • 4
  • Please refer this answer http://stackoverflow.com/questions/2928827/access-php-var-from-external-javascript-file – I'm Geeker Jun 13 '16 at 16:19
  • Do you have PHP short open tags enabled? – Jay Blanchard Jun 13 '16 at 16:19
  • @JayBlanchard Yes .. – Essam wageh Jun 13 '16 at 16:22
  • The HTML you are using to view the result seems to be completely unrelated to the one you've added the variable to. – apokryfos Jun 13 '16 at 16:22
  • @apokryfos All I want is to view the result of the java script function after passing the php parameter to it , When I remove the parameter from the function everything works , but when I add it back nothing works , that means the parameter can't be passed to the function from the PHP file and I don't know why .. – Essam wageh Jun 13 '16 at 16:25

1 Answers1

0

You wouldn't have access to the server JavaScript variable in the second HTML file unless you actually declare it in this file OR you somehow include the declaration.

<!DOCTYPE html>
<html>
<body>
<?php
$ser = "server";
?>
<script type="text/javascript">var server = "<?= $ser ?>";</script>

<script src="new4.js"></script>
<p>Click the button to demonstrate line-breaks in a popup box.</p>

<button onclick="myFunction()">Try it</button>

</body>
</html>

(new4.js wouldn't need to change).

PS: You may need to rename the file as PHP or ensure your webserver parses HTML as PHP.

Alternative approach (haven't tried it myself but should work).

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src='constants.php'></script>
<script src="new4.js"></script>
<p>Click the button to demonstrate line-breaks in a popup box.</p>

<button onclick="myFunction()">Try it</button>

</body>
</html>

constants.php

<?php
header('Content-Type: text/javascript');
$ser = "server";
echo "server = \"$ser\"";
apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Doesn't work ,, the problem is that , I made these simple codes to try to pass a parameter from an PHP file to a Java script file , In my real project I can't combine the both of them at one file , If I was able to do that then the problem should've been solved long time ago , but the passing operation is what I need and I can't do it , nothing works ,, – Essam wageh Jun 13 '16 at 16:37
  • @Essamwageh you have to somehow inject the PHP variables in your Javascript, problem being that PHP runs at server-side and JavaScript runs at client-side. These two can't communicate directly. I've added an alternative approach which may work, however I do think you should review what you're trying to do and consider alternative solutions to it. – apokryfos Jun 14 '16 at 08:06
  • Yup , that's exactly the problem , but that's ok I'll figure out someway to do what I want without doing any of this .. Thanks for everything .. :) – Essam wageh Jun 14 '16 at 15:48