Home > Back-end >  run php file with script tag
run php file with script tag

Time:03-31

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/Ky/vWYZiVPrhxIOQsl6VkyioGiy+4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz0oA7ZXT9CV1ut2ICfrquV9FoQ+jltIeJAcUnB+TMvTjn+Gs1lvh5bOIUUXYa0tIJCe+Je2LX38OpOLAJ+7U1h12rvXozelMT5SGd67wzUnFI7er3gJycSu7WAH72sUTT/Z/3nJQOZBOMHY8WyX8jqel5Mo8BMNLzIXHyjpA+iZlgYsEg==","img":"https://1.mbvnclick1.com/im?sid=2&data=ZQqgvmU6z8ZR4RPBdAhPWcdkbt5b+Wp435ln18YHYo1SXskUGSiZhGwhvcXnWECjuteCzRQRWIhfYTUDd4wLcUq7jKaYn55gJUbQZr3UM6SAx2dKKXUVQVmstTsIdXma7gZ57+8L58uusM7pf8HpgSTreH8rjJIX+QEruq544CQQF/TNxTpCAesrBgQpkUOL76hSB/0Eaw3yYO0mDUDR6zKLXkDo6cxruIRrER05RSFJVtlFr3ihmDZHJQZnl/O6","url":"https://1.mbvnclick1.com/clpsh?sid=2&d=1&data=h3OP98W8RXI52WXh0xUpzzPCqkn+oc1q7OZh2tb7pLxLU4il0MNlbTTRR/QJ3Ryj98kKbM2eOgq/VtMBpmy4huEGwavyp41rQdZTT/jdsu0QcYNMwUiNBH4mifSNaIzMTDYTeB9hZ8BPwGw/171wk2af2qmrmLi7e13XtfK+FpZltozDNAqS+DJkvH3SVKJHo8TkGjb2FQonQoXeVXqfp6jp2MYLqp/OFf6dOcERVM/f/YBgEZ2E/pzuMZSywxPt49sveDcfOE/9LOjBu6+U1XymVQdknq+0MzuJAd6Eq8/H4q/+7dlgvivqQm30C/vhG/fGSYQPEY+HdzAJZ/StRjZmMtGhsqHbMkGENTil4bzlo8VvMW6H2yLPpVVw8Eqw86jXlndl7qPusmT4W4VUVQzMEnKgDbiJFPGy45vE+3QOCqafNoCq90X7U/Llvr9Gxdox8qAUyhAMbqJU5p0GYlMk6iJDD3GaG/qAZN5hzM0=","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 > <img src="<?php echo $json['icn']; ?>"> </div> <div > <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

CodePudding user response:

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/Ky/vWYZiVPrhxIOQsl6VkyioGiy+4DYdpqaaMXlM5dVPkQoRzngoPAlvQ3w1pREOxlMjuR7DQHq6Yz0oA7ZXT9CV1ut2ICfrquV9FoQ+jltIeJAcUnB+TMvTjn+Gs1lvh5bOIUUXYa0tIJCe+Je2LX38OpOLAJ+7U1h12rvXozelMT5SGd67wzUnFI7er3gJycSu7WAH72sUTT/Z/3nJQOZBOMHY8WyX8jqel5Mo8BMNLzIXHyjpA+iZlgYsEg==","img":"https://1.mbvnclick1.com/im?sid=2&data=ZQqgvmU6z8ZR4RPBdAhPWcdkbt5b+Wp435ln18YHYo1SXskUGSiZhGwhvcXnWECjuteCzRQRWIhfYTUDd4wLcUq7jKaYn55gJUbQZr3UM6SAx2dKKXUVQVmstTsIdXma7gZ57+8L58uusM7pf8HpgSTreH8rjJIX+QEruq544CQQF/TNxTpCAesrBgQpkUOL76hSB/0Eaw3yYO0mDUDR6zKLXkDo6cxruIRrER05RSFJVtlFr3ihmDZHJQZnl/O6","url":"https://1.mbvnclick1.com/clpsh?sid=2&d=1&data=h3OP98W8RXI52WXh0xUpzzPCqkn+oc1q7OZh2tb7pLxLU4il0MNlbTTRR/QJ3Ryj98kKbM2eOgq/VtMBpmy4huEGwavyp41rQdZTT/jdsu0QcYNMwUiNBH4mifSNaIzMTDYTeB9hZ8BPwGw/171wk2af2qmrmLi7e13XtfK+FpZltozDNAqS+DJkvH3SVKJHo8TkGjb2FQonQoXeVXqfp6jp2MYLqp/OFf6dOcERVM/f/YBgEZ2E/pzuMZSywxPt49sveDcfOE/9LOjBu6+U1XymVQdknq+0MzuJAd6Eq8/H4q/+7dlgvivqQm30C/vhG/fGSYQPEY+HdzAJZ/StRjZmMtGhsqHbMkGENTil4bzlo8VvMW6H2yLPpVVw8Eqw86jXlndl7qPusmT4W4VUVQzMEnKgDbiJFPGy45vE+3QOCqafNoCq90X7U/Llvr9Gxdox8qAUyhAMbqJU5p0GYlMk6iJDD3GaG/qAZN5hzM0=","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>");

CodePudding user response:

You are directly mixing HTML inside php tags. You can close the php tag before using HTML tags.

<?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&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JDD3GaG/qAZN5hzM0=","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 >
        <img src="<?php echo $json['icn']; ?>">
      </div>
      <div >
        <img src="<?php echo $json['img']; ?>">
      </div> 
    </div>
  </div>
</a>`);
  • Related