Could you help me with a problem:
I want to execute PHP code within Javascript .
<html>
<head></head>
<body>
<script type="text/javascript">
<?php echo "hola como estan"; ?>);
</script>
</body>
</html>
The html output:
<html>
<head></head>
<body>
<script type="text/javascript">
hola como estan <-- marcado en rojo en el inspector del navegador
</script>
</body>
</html>
The error in the browser console:
Uncaught SyntaxError: Unexpected identifier
I think there is a misconception when it comes to structuring the task you want to do.
You don't actually run PHP inside Javascript , rather Javascript will make use of whatever PHP prints inside your tags or functions. Since PHP is a server language, it will be executed before Javascript .
Basic so that it is understood, the order in which they are executed:
1st PHP (server)
2nd HTML (browser)
3rd Javascript (browser)
So the flow would go as follows:
1.- What result do we want to achieve?
2.- What part of that result will PHP manage ?
3.- How does PHP print this text inside html ?
4.- How do you put everything into operation?
A simple example:
So as you have it is a syntax error. If you want to write something you can use the console like this:
Or also an alert like this:
You can use type (note the use of type="php" in the script that contains php code). The example is very insecure because it uses eval without a filter, so be careful. (This is basically what libraries like Babel use):
Currently I command like this
or tell me more about your concern, I will gladly help you.
In the script code you had put it as:
That's the problem, by putting echo you make the browser remove the php tags and put only the hello world. What you have to do you would have to mix php code with js. For example: If you want to output a console.log you have to do the following.