使用PHP和AJAX在服务器上存储和访问数据

时间:2022-06-08 20:22:32

I have a website that has the possibility to add comments which are just stored temporarily, when you reload the page the comments are gone. I save the comment data in an ObservAbleArrayList using knockout and javascript. One idea I had was to send this ObservAbleArrayList to my server, store it and then when the page is reloaded the stored arraylist would first update the commentfield. How could I do this with AJAX and PHP?

我有一个网站,可以添加临时存储的评论,当你重新加载页面评论消失。我使用knockout和javascript将注释数据保存在ObservAbleArrayList中。我有一个想法是将此ObservAbleArrayList发送到我的服务器,存储它,然后当重新加载页面时,存储的arraylist将首先更新注释字段。我怎么能用AJAX和PHP做到这一点?

Here is my javascriptcode for the comments:

这是我的评论的javascriptcode:

 function Comment() {
    var self = this;

    self.nickname = ko.observable();
    self.newMsg = ko.observable("");
    self.editable = ko.observable(false);

    self.addComment = function () {
       vm.comments.push(self);
       vm.selectedComment(new Comment());
    };

    self.deleteComment = function () {
        vm.comments.remove(self);
    };

    self.editComment = function () {
        self.editable(!self.editable());
    };
}

function ViewModel() {
  var self = this;
  self.comments = ko.observableArray();
  self.selectedComment = ko.observable(new Comment());
}

var vm = new ViewModel();
ko.applyBindings(vm);
});

Any help or examples would be very helpful! Thanks in advance.

任何帮助或示例都会非常有用!提前致谢。

1 个解决方案

#1


0  

Send the data as JSON to the server using jQuery as your bridge to handle the server side interaction with it's $.ajax() wrapper.

使用jQuery作为桥接器将数据作为JSON发送到服务器,以处理服务器端与其$ .ajax()包装器的交互。

First, you need to mutate the data into a JSON object to be sent over and easily parsed. In knockout, you can use the .toJSON(model) method on a ko object to get the JSON interpretation of it, such as:

首先,您需要将数据变更为JSON对象,以便发送并轻松解析。在淘汰赛中,您可以在ko对象上使用.toJSON(模型)方法来获取它的JSON解释,例如:

var jsonData = ko.toJSON(ViewModel);

Which will give you your JSON String. This is ready to be passed to the server, so now you can construct your $.ajax() call to your PHP script.

哪个会给你你的JSON字符串。这已准备好传递给服务器,因此现在您可以构建对PHP脚本的$ .ajax()调用。

$.ajax({
    url: '/path/to/my/script.ext',
    type: 'GET', //default anyway, provided for clarity
    dataType: 'json', //the returned data from the server will be automatically parsed as json
    data: jsonData, //the KO model we converted earlier
    success: function(data){
        //the server's response is in "data" above, jsonParsed already.
    }
});

#1


0  

Send the data as JSON to the server using jQuery as your bridge to handle the server side interaction with it's $.ajax() wrapper.

使用jQuery作为桥接器将数据作为JSON发送到服务器,以处理服务器端与其$ .ajax()包装器的交互。

First, you need to mutate the data into a JSON object to be sent over and easily parsed. In knockout, you can use the .toJSON(model) method on a ko object to get the JSON interpretation of it, such as:

首先,您需要将数据变更为JSON对象,以便发送并轻松解析。在淘汰赛中,您可以在ko对象上使用.toJSON(模型)方法来获取它的JSON解释,例如:

var jsonData = ko.toJSON(ViewModel);

Which will give you your JSON String. This is ready to be passed to the server, so now you can construct your $.ajax() call to your PHP script.

哪个会给你你的JSON字符串。这已准备好传递给服务器,因此现在您可以构建对PHP脚本的$ .ajax()调用。

$.ajax({
    url: '/path/to/my/script.ext',
    type: 'GET', //default anyway, provided for clarity
    dataType: 'json', //the returned data from the server will be automatically parsed as json
    data: jsonData, //the KO model we converted earlier
    success: function(data){
        //the server's response is in "data" above, jsonParsed already.
    }
});