내 스크립트 작동 방식에 문제가 있습니다. 일어날 일은 다음과 같습니다. 폼이로드되면 .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 시간 비교 당신은 "이 마지막 부분은"구원을 수행 말을 내 현재 코드
감사합니다. @ ed-cotterll 내 논리 오류입니다. –
@JonathanBishop 도움을 주어서 기쁩니다! 도움이된다면 대답을 수락하는 것을 잊지 마십시오. –
그래서 순서를 바꾸고 if isset을 to에 넣으면 json 파일이 초기 페이지로드시 지워지지 않게됩니다. 다시 한번 감사드립니다 @ –