HTML在调用PHP函数后消失

huangapple go评论84阅读模式
英文:

HTML disappearing after PHP function called

问题

这显示了按钮按下的结果。思路是它从文件中读取并写入,以便不同浏览器中的用户可以看到相同的结果。

最初我在访问DOM时遇到了一些问题,但后来发现,在第一次按钮按下后,我的PHP脚本下面的一切都被清除了,“在按钮点击后,这个区域不会显示”。

简单的解决方法是将DOM元素简单地移动到更高的位置,但是...我想知道...

为什么这个区域会被删除?就好像它被当作PHP一样对待。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->
// 背景循环
function updateChat() {
    var file = 'word.txt?v=' + Date.now();
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function () {
        if (rawFile.readyState === 4) {
            if (rawFile.status === 200 || rawFile.status == 0) {
                var allText = rawFile.responseText;
                console.log(allText);
                document.getElementById("shownword").innerHTML = allText;
            }
        }
    }
    rawFile.send(null);
    setTimeout(updateChat, 500);
}

<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>Random Word Generator</title>

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
    <script type="text/javascript"></script>
    <script language="javascript" src="update.js" type="text/javascript"></script>
</head>

<body onload="updateChat();">

    <p id="shownword"></p>

    <form action="" method="post">
        <input type="submit" name="word" class="button" value="Button1" id="button1" />
        <input type="submit" name="word" class="button" value="Button2" id="button2" />
    </form>

    <?php
    // 当按钮被按下时,将调用此函数
    function postword(){
        $fp = fopen('word.txt', 'w');
        fwrite($fp, $_POST['word']);
        fclose($fp);
    }
    // 当按钮被按下时,将调用函数
    if (isset($_POST['word'])) {
        postword();
        return;
    }
    ?>

    This area doesn't get shown after button click

</body>
</html>

<!-- end snippet -->

这是您提供的代码的翻译部分。

英文:

This displays the result of a button press. The idea is that it reads and writes from a file so that users in different browsers can see the same result.

I had some issues accessing the DOM originally but then discovered that after the first button press, everything below my php script is wiped This area doesn&#39;t get shown after button click

The easy workaround is to simply move the DOM element higher up but... I want to know...

Why is this area getting deleted? It's as if it's being treated like PHP.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

//Background Loop
function updateChat(){
var file = &#39;word.txt?v=&#39; + Date.now() ;
var rawFile = new XMLHttpRequest();
rawFile.open(&quot;GET&quot;, file, false);
rawFile.onreadystatechange = function ()
{
if(rawFile.readyState === 4)
{
if(rawFile.status === 200 || rawFile.status == 0)
{
var allText = rawFile.responseText;
console.log(allText);
document.getElementById(&quot;shownword&quot;).innerHTML = allText;
}
}
}
rawFile.send(null);
setTimeout(updateChat, 500);
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;title&gt;Random Word Generator&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script language=&quot;javascript&quot; src=&quot;update.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body onload=&quot;updateChat();&quot;&gt;
&lt;p id=shownword&gt;&lt;/p&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
&lt;input type=&quot;submit&quot; name=&quot;word&quot;
class=&quot;button&quot; value=&quot;Button1&quot; id=&quot;button1&quot;/&gt; 
&lt;input type=&quot;submit&quot; name=&quot;word&quot;
class=&quot;button&quot; value=&quot;Button2&quot; id=&quot;button2&quot;/&gt; 
&lt;/form&gt;
&lt;?php
//This function gets called when button is pushed
function postword(){
$fp = fopen(&#39;word.txt&#39;, &#39;w&#39;);
fwrite($fp, $_POST[&#39;word&#39;]);
fclose($fp);
}
//When the button is pushed, the function will be called
if (isset($_POST[&#39;word&#39;])) {
postword();
return;
}
?&gt;
This area doesn&#39;t get shown after button click
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 1

只删除"return;"。

if(isset($_POST['word'])) {
  postword();
  // return;
}
英文:

Just remove the "return;"

if(isset($_POST[&#39;word&#39;])) {
postword();
// return;
}

huangapple
  • 本文由 发表于 2020年1月3日 17:03:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59575678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定