Can I use php in javascript?

后端 未结 7 451
萌比男神i
萌比男神i 2021-01-25 23:49

Begginer\'s question.

Can I use PHP in javascript?

For example in jquery,

...
...
triggers.eq(1).post(\'

        
相关标签:
7条回答
  • 2021-01-26 00:12

    PHP is executed on the server side and JavaScript on the client side. But you can print the PHP variable like this:

    triggers.eq(1).post(<?php echo json_encode('admin/categories/delete/'. $list['id']); ?>);
    

    And then the output is like:

    triggers.eq(1).post('admin/categories/delete/12345');
    
    0 讨论(0)
  • 2021-01-26 00:12

    php runs on the server.

    typically, javascript runs in a browser.

    You can use jQuery to make a request for an url which is handled by php, and thus have jQuery delete!

    0 讨论(0)
  • 2021-01-26 00:12

    You can use PHP to write your javascript.
    You can't execute PHP client-side - so no, you can't use php in your javascript code.

    0 讨论(0)
  • 2021-01-26 00:13

    No, You cannot send query that way. Even you can send php codes to server, and exec it, its not secure way.

    0 讨论(0)
  • 2021-01-26 00:15

    What you want to do is possible, if the javascript is in an PHP file,

    triggers.eq(1).post('admin/categories/delete/<?= $list['id'] ?>');
    

    The PHP doenst run in the javascript, but the variable is replaced when the page is sent to the client.

    0 讨论(0)
  • Rename your files with the extension of PHP and then you can use PHP code inside of your file.

    Another solution to tell your server to render your JS files as PHP script. For some performance and security reasons it's not recommended.

    0 讨论(0)
提交回复
热议问题