任务未通过PHP保存在JSON FILE中 [英] Task not saved in JSON FILE through PHP

查看:49
本文介绍了任务未通过PHP保存在JSON FILE中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的html文件

This is my html file

<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="content-type" content="text/html" charset="utf-8"/>
		<title>WELCOME TO TODO LIST</title>
		<script src="jquery3.1.1.js"></script>
		<script src="ToDoListInProgessScriptc.js"></script>
	</head>
	<body>
		<h1>My To-Do List</h1>
		<form>
			<fieldset><legend>Task:</legend>
			<div>
			<div>
			<input type="text" id="task" placeholder="what needs to be done?">
			</div>
			<div>
			<input type="button" value="ADD TASK" id="button">
			</div>
			</div>
			</fieldset>
		</form>
		<ul id="taskslist"></ul>
	</body>
</html>



这是我的js文件 ToDoListInProgressjs.js


This is my js file ToDoListInProgressjs.js

function Todo(task)
{
	this.task = task;
	this.done = false;
}

var todos = new Array();

$(document).ready(function()
	{
	$('#button').click(getFormData);
	getTodoData();
});

function getTodoData()
{
	$.ajax({
		url: "ToDoListInProgressJsonc.json",
        dataType: "text",
        success: function(jdata){
			alert(jdata);
			var jsonData = $.parseJSON(jdata);
			for (var i = 0; i < jsonData.length; i++)
				{
					var todoItem = jsonData[i];
					todos.push(todoItem);
				}
			addToDosToPage();		
			}
           });
}

function addToDosToPage()
{
	$.each(todos,function(index,tasks){
		$('#taskslist').append('<li>'+tasks.task+'<lable><input type="checkbox"></lable><br/></li>');
		});
}

function getFormData()
{	
	var task=$("input:text").val();
	if (checkInputText(task, "Please enter a task")) return;
	console.log("New task: " + task);
	var todoItem = new Todo(task);
	todos.push(todoItem);
	addToDoToPage(todoItem);
	saveTodoData();
}

function checkInputText(value, msg)
{
	if (value == null || value == "")
	{
		alert(msg);
		return true;
	}
	return false;
}

function addToDoToPage(todoItem)
{	
	$('#taskslist').append('<li>'+todoItem.task+'<lable><input type="checkbox"></lable><br/></li>');
	//document.forms[0].reset();
}

function saveTodoData()
{
	var todoJSON = JSON.stringify(todos);
	var request = new XMLHttpRequest();
	var URL = "save.php?data=" + encodeURI(todoJSON);
	request.open("GET", URL);
	request.setRequestHeader("Content-Type","text/plain;charset=UTF-8");
	request.send();
}



这是我的json文件 ToDoListInProgressJsonc.json


This is my json file ToDoListInProgressJsonc.json

[{"task":"get milk","done":false},
{"task":"get broccoli","done":false},
{"task":"bring rian","done":false},
{"task":"go to school","done":false},
{"task":"buy milk","done":false}]



这是我的php文件 save.php


This is my php file save.php

<?php
if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) 
{
	function strip_slashes($input)
	{
	if (!is_array($input))
		{
			return stripslashes($input);
		}
	else
		{
			return array_map('strip_slashes', $input);
		}
	}
	$_GET = strip_slashes($_GET);
	$_POST = strip_slashes($_POST);
	$_COOKIE = strip_slashes($_COOKIE);
	$_REQUEST = strip_slashes($_REQUEST);
}
function customError($errno, $errstr)
{
	echo "Error: [$errno] $errstr<br>";
	echo "Ending Script";
	die("Ending Script");
} set_error_handler("customError");
$myData = $_GET["data"];
$myFile = "ToDoListInProgressJson.json";
$fileHandle = fopen($myFile, "a");
fwrite($fileHandle, $myData);
fclose($fileHandle);
?>





我有什么试过:



当我刷新浏览器页面时我的新任务消失了这意味着任务没有保存在Json文件中...

请解释我哪里出错。



What I have tried:

when I refresh the browser page my new task disappears That means task is not saved in Json file...
Please Explain where am I going wrong.

推荐答案

document )。ready( function ()
{
(document).ready(function() {


' #button')。click(getFormData);
getTodoData();
});

function getTodoData()
{
('#button').click(getFormData); getTodoData(); }); function getTodoData() {


.ajax({
url: ToDoListInProgressJsonc.json
dataType: text
成功: function (jdata){
alert(jdata);
var jsonData =
.ajax({ url: "ToDoListInProgressJsonc.json", dataType: "text", success: function(jdata){ alert(jdata); var jsonData =


这篇关于任务未通过PHP保存在JSON FILE中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆