0

I have been reading through various questions relating to this, and thanks to a general lack of programming knowledge despite my best efforts I have not been able to make this work. Here are the questions I was looking at:

Update text file on button click

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

And here is the code I am trying to use.

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

    }
?>    

I have no idea what I am doing wrong, if anyone could take a look I would be very grateful.

EDIT: I ended up using a different method to solve my problem, but thank you for your help. I checked back after finding a solution, but your fix would have worked too George. Thanks everyone.

Community
  • 1
  • 1
Warior4356
  • 27
  • 5

2 Answers2

1

Change your buttons to inputs. e.g.

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

This way their values will be posted when the form is submitted

GeorgeQ
  • 1,382
  • 10
  • 8
0
<input type="submit" name="on" value="ON">
<input type="submit" name="off" value="OFF">

Instead

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