开发者

Can I use php in javascript?

Begginer's question.

Can I use PHP in javascript?

For example in jquery,

...
...
triggers.eq(1).post('<?php admin/categories/delet开发者_如何学Ce/'. $list['id']; ?>);


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');


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.


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!


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


No, you cannot use PHP within JavaScript. Think about it this way: you need a PHP interpreter/runtime to execute the PHP, and browsers do not contain this. Based on your code snippet above, you could instead send an ajax call to a PHP script on the server.

I suppose you could pass PHP code to a server-side script and interpret/execute it, but you'd still need ajax or a full GET/POST request, and it would never be a good thing to do because of how easy it would be for malicious code injection.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜