Skip to content
Advertisement

run php file with script tag

Is there any way to insert php code through <script>? Like `<script src=”example.php”>. I know that script is only for JS but I want PHP to generate JS that I can insert with script tag. Here I found a possible sollution: Generate JavaScript file with PHP when requested in HTML. PHP file:

JavaScript

But I have an error Uncaught SyntaxError: Unexpected token '<' because in php file I have HTML and JS finds it as an error.

I found how to do this here:Generate JavaScript file with PHP when requested in HTML. But I have error

Advertisement

Answer

When need to generate a JS script via PHP, you should think in JS way. To add an HTML element from js you must use some functions like document.write().

Your sample code changed like this:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement