简体   繁体   English

PHP-读取文本文件时出现问题

[英]PHP - Problems reading a text file

So recently I improved my code and managed to save a javascript variable through PHP(AJAX) to a txt file. 因此,最近我改进了代码,并设法通过PHP(AJAX)将javascript变量保存到txt文件中。 The txt is working and it gets the javascript number but I'm having problems retrieving the number on the txt file. txt工作正常,它获取了javascript编号,但是在txt文件上检索编号时遇到问题。

PHP code: PHP代码:

<?php
 $file_content = file_get_contents('num.txt');
?>

Javascript code: JavaScript代码:

function recnum(){
document.getElementById("num").innerHTML = '<?php echo $file_content; ?>';
}

So after this, the number on the html page stays the same (1) and doesn't change. 因此,此后,html页上的数字保持不变(1),并且没有变化。

If you want to put data directly into HTML avoiding Javascript then you can try the code following: 如果要避免使用Javascript将数据直接放入HTML中,则可以尝试以下代码:

<div id="num"><?php echo $file_content; ?></div>

Note: I assumed that you have div with num id already in html. 注意:我假设您已经在html中使用num id的div。

UPDATE: Based on your comment. 更新:根据您的评论。 You have defined variable after it being used. 使用后已定义变量。 So define it first and then use like in modified code below: 因此,首先定义它,然后在修改后的代码中使用如下代码:

<?php
$file = "num.txt";
//Path to your *.txt file 
$contents = file($file);
$file_content = implode($contents);
echo $file_content;
?> 

<html> 
    <body> 
    <head> 
        <title> Botão </title> 
        <link rel="stylesheet" href="css/style.css">
        <meta charset="UTF-8"> 
        <script type="text/javascript" src="jquery-3.3.1.min.js"></script> 
    </head>
    <div id="num"><?php echo $file_content; ?></div>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM