2016-10-14 5 views
0

私のスクリプトの動作に問題があります。起こりそうなのは、フォームがロードされると、最後に使用されたデータを.jsonファイルから読み込み、フィールドにデータを取り込みます。それは動作しますが、新しいデータが送信されると、新しいデータではなく古いデータがフォームに入力されます。PHPファイルからデフォルト値を読み取る

最初の部分は、JSONファイルを読み込んで、この次のセクションでは、クリアボタンが

<?php if ($_POST['task'] == 'Clear'){ 
    $time = ''; 
    $event = ''; 
    $notes = ''; 
    $remainingtime = ''; 
$task = 'clear';} 
    else { 
?> 

この最後のセクションでは、新しい配列を移入して押された場合にどうするかを決める

<?php $old = json_decode((file_get_contents(dirname(__FILE__)."/json.json")), true); ?> 
<!--Building the form--> 
    <table> 
     <form action="edit.php" method="POST"> 
      <label for="time">Time:</label> 
      <input type="text" name="time" id="time" value="<?php echo ($old['time']); ?>"></input> 
      <br /> 
      <label for="event">Event:</label> 
      <input type="text" name="event" id="event" value="<?php echo ($old['event']); ?>"></input> 
      <br /> 
      <label for="event">Notes:</label> 
      <input type="textarea" name="notes" id="notes" value="<?php echo ($old['notes']); ?>"></input> 
      <br /> 
      <input type="submit" name="task" value="Go" /> 
      <input type="submit" name="task" value="Clear" /> 
     </form> 

フォームを構築しますこれをJSONファイルとして保存します。

<?php if ($old['time'] != $_POST['time']){ 
    $time = preg_replace("/[^0-9]/","",$_POST['time']); 
    $remainingtime = ((microtime())+($timein*60)*1000); 
    $task = 'countdown';} 
    else { 
    $remainingtime = $old['time']; 
    } 

    $event = $_POST['event']; 
    $notes = $_POST['notes']; 
    $remainingtime = ''; 
    $task = 'display'; 
    } 

    $new = array ('time' => $time, 'event' => $event,'notes' => $notes , 'remainingtime' => $remainingtime , 'task' => $task); 


    file_put_contents((dirname(__FILE__).'/json.json'), json_encode($new)); 
} 

?> 

クリアボタンを繰り返し押すと、フォームが消去され、JSONファイルが消去されます。 「実行」ボタンを押すと、フォームとJSONファイルが新しいフォームの提出とJSONファイルの内容との間で交互に表示されます。

ご協力いただければ幸いです。

NB時間の比較は、あなたが、「この最後のセクションは、」貯蓄をすると言う私の現在のコード

答えて

2

では動作しません。フォームを生成した後で、ページの上部にデータをロードして下部に保存すると、ロードされたデータは常にと表示される古いデータになります。までファイルを更新しないでください。の後に古いデータをロードして表示しました。この問題を解決するには

、この基本的な構造から、あなたのコードを変更します。これに

load data 
display form using old data 
decide what to do if the clear button is pressed 
populate the new array 
save the new array as the json file 

:ところで

decide what to do if the clear button is pressed 
if there's new data: 
    populate the new array 
    save the new array as the JSON file 
otherwise, load the old data 
display form using the data (whether it's new or old 

$post['time']へのあなたの比較は意味がありません。共有しているコードに$postではなく、$_POSTと同じですが、と混同しています)を定義していません。

+0

ありがとうございます。@ ed-cotterll私のところは論理的なエラーでした。 –

+0

@JonathanBishop喜んで助けてください!それが役に立つとわかったら、その答えを受け入れることを覚えておいてください。 –

+0

私は、最初のページの読み込み時にjsonファイルが消去されるのを防ぐために、順序を逆にしてifsetをtoに配置しました。再度ありがとう@ –

0

問題は、$_POSTのデータがまだ存在することです。フォームを再送信すると、古いデータがもう一度挿入されます。あなたがする必要があることは、フォームデータをjsonファイルに書き込んだ後にページをリダイレクトすることです。

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    switch ($_POST['action']) { 
     case 'Clear': 
      // Do clear stuff 
      break; 
     case 'Go': 
     default: 
      // Do go stuff 
      break; 
    } 

    // Redirect 
    header("Location: ".$_SERVER['REQUEST_URI']); 
    exit; 
} 
関連する問題