Solution to the problem that js cannot run php: 1. Specify the format file; 2. Modify the code writing to "
< ;/body>"; 3. Get and run it through "var serverData = document.body.getAttribute('serverData');".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
What should I do if js cannot run php?
Problem description:
Placing php in js does not execute
function gowechat (){ window.location.href="<?php $str=file_get_contents('http://weixin.qq.zoooe.com/?u=7nn7jj72rlms'); echo $str; ?>;" }
It is possible to write directly in the page Yes, but it won’t work when placed in .js. What’s going on?
Solution:
Only files in the format specified by the server will be parsed by the php server. js files are definitely not supported by the server, so no.
If it is an apache server, please check the
httpd.conf: <FileMatch '.php$'> ... </FileMatch>
part;
If it is nginx, please check the
nginx.conf: server { location ~ \.php$ { } }
I personally do not recommend this writing method. If you need to get some data from the server, you can do this:
<body data-serverData="<?php echo $data; ?>"></body>
Then get it through js:
var serverData = document.body.getAttribute('serverData');
My suggestion is that it is best to use the PHP template engine to handle the data interaction between the server code and the view. Question.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of What should I do if js cannot run php?. For more information, please follow other related articles on the PHP Chinese website!