0

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>
4

2 に答える 2

2

ロード時: PHP が実行されます。値はファイルに入れられます。

クリック時: JS 変数が変更されます。js ファイルまたは制御 php メソッドからファイルを変更する要求がないため、ファイルの値は変更されません。

考えられる解決策: 「+」または「-」をクリックすると、非同期 js リクエストが php コントローラーに送信さfile_put_contentsれ、「Data.txt」に新しい値が書き込まれます。

于 2013-11-09T22:21:15.040 に答える
0

ajax 呼び出しを使用して、ファイル更新コードを ajax ターゲット php ファイルに入れます。クリックするたびにページをリロードするのではなく、ajax リクエストを送信するようにします。

于 2013-11-09T22:13:33.750 に答える