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:
<?php header('Content-Type: text/javascript'); $json = '{"title":" \ud83c\uddf7\ud83c\uddfa \u0420\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u043e\u0435 \u043a\u0430\u0437\u0438\u043d\u043e #1 \ud83c\uddf7\ud83c\uddfa ","desc":"\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0439\u0441\u044f \u0438 \u0438\u0433\u0440\u0430\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0437\u0430 \u043d\u0430\u0448 \u0441\u0447\u0451\u0442","icn":"https://1.mbvnclick1.com/ic?sid=2&data=0eikgji0Ck2EKXJkLTJfLie%2FKy%2FvWYZiVPrhxIOQsl6VkyioGiy%2B4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz0oA7ZXT9CV1ut2ICfrquV9FoQ%2BjltIeJAcUnB%2BTMvTjn%2BGs1lvh5bOIUUXYa0tIJCe%2BJe2LX38OpOLAJ%2B7U1h12rvXozelMT5SGd67wzUnFI7er3gJycSu7WAH72sUTT%2FZ%2F3nJQOZBOMHY8WyX8jqel5Mo8BMNLzIXHyjpA%2BiZlgYsEg%3D%3D","img":"https://1.mbvnclick1.com/im?sid=2&data=ZQqgvmU6z8ZR4RPBdAhPWcdkbt5b%2BWp435ln18YHYo1SXskUGSiZhGwhvcXnWECjuteCzRQRWIhfYTUDd4wLcUq7jKaYn55gJUbQZr3UM6SAx2dKKXUVQVmstTsIdXma7gZ57%2B8L58uusM7pf8HpgSTreH8rjJIX%2BQEruq544CQQF%2FTNxTpCAesrBgQpkUOL76hSB%2F0Eaw3yYO0mDUDR6zKLXkDo6cxruIRrER05RSFJVtlFr3ihmDZHJQZnl%2FO6","url":"https://1.mbvnclick1.com/clpsh?sid=2&d=1&data=h3OP98W8RXI52WXh0xUpzzPCqkn%2Boc1q7OZh2tb7pLxLU4il0MNlbTTRR%2FQJ3Ryj98kKbM2eOgq%2FVtMBpmy4huEGwavyp41rQdZTT%2Fjdsu0QcYNMwUiNBH4mifSNaIzMTDYTeB9hZ8BPwGw%2F171wk2af2qmrmLi7e13XtfK%2BFpZltozDNAqS%2BDJkvH3SVKJHo8TkGjb2FQonQoXeVXqfp6jp2MYLqp%2FOFf6dOcERVM%2Ff%2FYBgEZ2E%2FpzuMZSywxPt49sveDcfOE%2F9LOjBu6%2BU1XymVQdknq%2B0MzuJAd6Eq8%2FH4q%2F%2B7dlgvivqQm30C%2FvhG%2FfGSYQPEY%2BHdzAJZ%2FStRjZmMtGhsqHbMkGENTil4bzlo8VvMW6H2yLPpVVw8Eqw86jXlndl7qPusmT4W4VUVQzMEnKgDbiJFPGy45vE%2B3QOCqafNoCq90X7U%2FLlvr9Gxdox8qAUyhAMbqJU5p0GYlMk6iJDD3GaG%2FqAZN5hzM0%3D","price":"0.0055"}'; $json = preg_replace('/[[:cntrl:]]/', '', $json); $json = json_decode($json, true); ?> <a href="https://airlinetravel.life/"> <div> <div> <p> <?php echo $json['title']; ?> </p> <p> <?php echo $json['desc']; ?> </p> <div class="iconBanner"> <img src="<?php echo $json['icn']; ?>"> </div> <div class="imgBanner"> <img src="<?php echo $json['img']; ?>"> </div> </div> </div> </a> ?>
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:
<?php header('Content-Type: text/javascript'); $json = '{"title":" \ud83c\uddf7\ud83c\uddfa \u0420\u043e\u0441\u0441\u0438\u0439\u0441\u043a\u043e\u0435 \u043a\u0430\u0437\u0438\u043d\u043e #1 \ud83c\uddf7\ud83c\uddfa ","desc":"\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u0443\u0439\u0441\u044f \u0438 \u0438\u0433\u0440\u0430\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437 \u0437\u0430 \u043d\u0430\u0448 \u0441\u0447\u0451\u0442","icn":"https://1.mbvnclick1.com/ic?sid=2&data=0eikgji0Ck2EKXJkLTJfLie%2FKy%2FvWYZiVPrhxIOQsl6VkyioGiy%2B4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz0oA7ZXT9CV1ut2ICfrquV9FoQ%2BjltIeJAcUnB%2BTMvTjn%2BGs1lvh5bOIUUXYa0tIJCe%2BJe2LX38OpOLAJ%2B7U1h12rvXozelMT5SGd67wzUnFI7er3gJycSu7WAH72sUTT%2FZ%2F3nJQOZBOMHY8WyX8jqel5Mo8BMNLzIXHyjpA%2BiZlgYsEg%3D%3D","img":"https://1.mbvnclick1.com/im?sid=2&data=ZQqgvmU6z8ZR4RPBdAhPWcdkbt5b%2BWp435ln18YHYo1SXskUGSiZhGwhvcXnWECjuteCzRQRWIhfYTUDd4wLcUq7jKaYn55gJUbQZr3UM6SAx2dKKXUVQVmstTsIdXma7gZ57%2B8L58uusM7pf8HpgSTreH8rjJIX%2BQEruq544CQQF%2FTNxTpCAesrBgQpkUOL76hSB%2F0Eaw3yYO0mDUDR6zKLXkDo6cxruIRrER05RSFJVtlFr3ihmDZHJQZnl%2FO6","url":"https://1.mbvnclick1.com/clpsh?sid=2&d=1&data=h3OP98W8RXI52WXh0xUpzzPCqkn%2Boc1q7OZh2tb7pLxLU4il0MNlbTTRR%2FQJ3Ryj98kKbM2eOgq%2FVtMBpmy4huEGwavyp41rQdZTT%2Fjdsu0QcYNMwUiNBH4mifSNaIzMTDYTeB9hZ8BPwGw%2F171wk2af2qmrmLi7e13XtfK%2BFpZltozDNAqS%2BDJkvH3SVKJHo8TkGjb2FQonQoXeVXqfp6jp2MYLqp%2FOFf6dOcERVM%2Ff%2FYBgEZ2E%2FpzuMZSywxPt49sveDcfOE%2F9LOjBu6%2BU1XymVQdknq%2B0MzuJAd6Eq8%2FH4q%2F%2B7dlgvivqQm30C%2FvhG%2FfGSYQPEY%2BHdzAJZ%2FStRjZmMtGhsqHbMkGENTil4bzlo8VvMW6H2yLPpVVw8Eqw86jXlndl7qPusmT4W4VUVQzMEnKgDbiJFPGy45vE%2B3QOCqafNoCq90X7U%2FLlvr9Gxdox8qAUyhAMbqJU5p0GYlMk6iJDD3GaG%2FqAZN5hzM0%3D","price":"0.0055"}'; $json = preg_replace('/[[:cntrl:]]/', '', $json); $json = json_decode($json, true); ?> document.write("<a href="https://airlinetravel.life/"> <div> <div> <p> <?php echo $json['title']; ?> </p> <p> <?php echo $json['desc']; ?> </p> <div class="iconBanner"> <img src="<?php echo $json['icn']; ?>"> </div> <div class="imgBanner"> <img src="<?php echo $json['img']; ?>"> </div></div> </div> </a>");