2016-10-14 2 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 파일이 지워집니다. Go 버튼을 누르면 양식과 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

그래서 순서를 바꾸고 if isset을 to에 넣으면 json 파일이 초기 페이지로드시 지워지지 않게됩니다. 다시 한번 감사드립니다 @ –

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; 
}