user2957574
user2957574

Reputation: 11

How do I edit Data in a text file?

I have a ticker, that has two buttons that raise and lower the value by one each time the corresponding button is clicked. I would also like this to be equal with the value of a number located in a text file. For example, the value of both the counter and the file are zero. I press the "+1" button, now both the counter and the number in the file are equal to 1. How might i do this? So far this is all i have been able to do because php is all executed at once.

    <!DOCTYPE html>
    <html>
    <head>
    <title>Ticker</title>
    <link rel="stylesheet" type="text/css" href="../resources/css/ticker.css" />
    <script src="../resources/js/ticker.js"></script>
    </head>
    <body>
    <div class="box">    
        <label for="qty"><abbr title="Quantity">Qty</abbr></label>
        <input id="qty" value="<?php echo file_get_contents('Data.txt'); ?>" />
        <button id="down" onclick="modify_qty(-1)">-1</button>
        <button id="up" onclick="modify_qty(1)">+1</button>
    </div>
    </body>
    </html>

Upvotes: 0

Views: 62

Answers (2)

Cristian Cavalli
Cristian Cavalli

Reputation: 2699

On load: PHP is executed; value is put in file.

On click: JS vars are changed; file value is not modified because there is no request to modify the file, either from the js file or controlling php method.

Possible Solution: When '+' or '-' is clicked send an asynchronous js request to php controller and have file_put_contents write new value to 'Data.txt`

Upvotes: 2

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

Use an ajax call and put the the file updation code in the ajax target php file. So that each time you click, you will not be reloading the page but sending ajax requests.

Upvotes: 0

Related Questions