Home >Backend Development >PHP Problem >What should I do if the js output by PHP is not executed?
The js output by PHP is not executed
<?php echo "<script>alert('我弹出来了')</script>"; ?>
You can output the JS script in PHP to the browser in one sentence implement. (Recommendation: "PHP Tutorial")
But today I encountered a very strange problem, that is, this code just outputs a string under Chrom and Firefox, and is not executed. An alert pops up.
Principle:
MIME chrome does not parse text/plain. PHP can use header to output html, and the browser uses the last output header as content-type
header('Content-Type:text/html;charset=utf-8');
text/plain html will not be executed.
Just need to re-header before alert.
if(!uploadFile()) { echo "<script>alert('上传文件失败')</script>"; } header('Content-Type:text/html;charset=utf-8'); echo ("")
The above is the detailed content of What should I do if the js output by PHP is not executed?. For more information, please follow other related articles on the PHP Chinese website!