"; 3. Pass "var "serverData = document.body.getAttribute('serverData');" can be obtained and run."/> "; 3. Pass "var "serverData = document.body.getAttribute('serverData');" can be obtained and run.">

Home  >  Article  >  Backend Development  >  What should I do if js cannot run php?

What should I do if js cannot run php?

藏色散人
藏色散人Original
2022-11-08 10:15:041976browse

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

What should I do if js cannot run php?

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(&#39;http://weixin.qq.zoooe.com/?u=7nn7jj72rlms&#39;); echo $str; ?>;" }

What should I do if js cannot run php?

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 &#39;.php$&#39;> ... </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(&#39;serverData&#39;);

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn