【问题标题】:Trying to make a php script to control a button that will change a value in a txt file尝试制作一个 php 脚本来控制一个按钮,该按钮将更改 txt 文件中的值
【发布时间】:2015-11-18 11:44:51
【问题描述】:

我一直在阅读与此相关的各种问题,尽管我尽了最大的努力,但由于普遍缺乏编程知识,我无法完成这项工作。以下是我正在查看的问题:

Update text file on button click

How to edit the contents of a file on button click? PHP?

这是我尝试使用的代码。

<h2>Click</h2>
<form action="" method="post">
<button name="on" class="on">ON</button> <button name="off" class="off">OFF</button>
</form>

<?php
if(isset($_POST['on']))
{
        $file = 'LED.txt';
        $previous = file_get_contents($file);


            file_put_contents($file, '1');

    }
?>

<?php
if(isset($_POST['off']))
{
        $file = 'LED.txt';
        $previous = file_get_contents($file);


           file_put_contents($file, '0');

    }
?>    

我不知道我做错了什么,如果有人可以看看我将非常感激。

编辑:我最终使用不同的方法来解决我的问题,但感谢您的帮助。找到解决方案后我检查了一遍,但乔治你的解决方法也可以。谢谢大家。

【问题讨论】:

  • 运行脚本时会发生什么?你想发生什么?

标签: php html


【解决方案1】:
<input type="submit" name="on" value="ON">
<input type="submit" name="off" value="OFF">

相反

<button name="on" class="on">ON</button>
<button name="off" class="off">OFF</button>

【讨论】:

    【解决方案2】:

    将您的按钮更改为输入。例如

    <input type="submit" name="on" value="On" />
    <input type="submit" name="off" value="Off" />
    

    这样他们的值将在表单提交时发布

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-23
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      相关资源
      最近更新 更多