[英]How to save a JavaScript string on a server with PHP and request it again later?
所以我的问题实际上很简单:
我有此功能(简体)-单击按钮时触发:
$search.onclick = function () {
// let's just say the user did put in a valid documentname
// something like "John"
documentname = $('#userinput').val();
url = "https://example.api.url.com/" + documentname + "&format=json"
// looks weird but "source = $.getJSON(url).done..." works like expected
source = $.getJSON(url).done(function () {
sourcestring1 = JSON.stringify(source);
// this findid(); below is just a function that takes the id i'm looking for
// out of the just stringified JSON (works also), it does "id = "123456"
// (just a number but in string format)
findid();
// id has a value now ("123456"), so lets console.log it
console.log(id);
});
};
在findid();
被执行并且id
有一个值,我想将此值另存为服务器上的可读文件。 文件名应与来自其的文档名称相同(在本例中为John
)。 文件的内容应该只是id
的值(在这种情况下为123456
)。 哪种文件格式? 我不知道。 这是下一个问题...
我希望做的下一件事是,当另一个用户再次输入确切的文档名时,加载此生成的文件。 因为如果文件已经存在,则无需再次加载整个JSON。 因此,更新后的代码将如下所示(我知道这不是实际的代码,但也许更容易理解):
$search.onclick = function () {
// another user inputs "John"
documentname = $('#userinput').val();
// so we want to prove if a file called "John" already exists on the server
if ([A FILENAME ON THE SERVER] == documentname) {
// if it exists, we just have to open that file and take the content (the id we saved before) out of it
[OPEN FILE/TAKE CONTENT]
// and just assign it to the variable
id = [CONTENT OF FILE]
// console.log it
console.log(id);
}
else {
// if it doesn't already exist, just run the normal code
url = "https://example.api.url.com/" + documentname + "&format=json"
source = $.getJSON(url).done(function () {
sourcestring1 = JSON.stringify(source);
findid();
// console.log it
console.log(id);
// and of course save this now on the server
[SOME PHP ACTION HERE]
});
}
};
$.get
尝试了很多事情,但我不知道如何使用PHP处理已发送的事情。
我不知道哪种文件格式最适合此操作,或者甚至是否可能。
我不知道如何将变量从JavaScript传输到PHP文档,或者在那里如何使用它们,反之亦然。
您可以使用localStorage
将文件存储在用户浏览器配置文件夹:用户文件系统中。 如果文件存在于localStorage
,使用来自文件localStorage
未做$.getJSON()
请求,否则调用$.getJSON()
; success
$.getJSON()
将文件设置为localStorage
。
$search.onclick = function () {
// another user inputs "John"
documentname = $('#userinput').val();
// so we want to prove if a file called "John" already exists on the server
if (localStorage.getItem(documentname) != null) {
// if it exists, we just have to open that file and take the content (the id we saved before) out of it
// and just assign it to the variable
id = localStorage.getItem(documentname);
// console.log it
console.log(id);
}
else {
// if it doesn't already exist, just run the normal code
// and of course save this now on the server
// [SOME PHP ACTION HERE]
url = "https://example.api.url.com/" + documentname + "&format=json"
source = $.getJSON(url).done(function () {
sourcestring1 = JSON.stringify(source);
findid();
// console.log it
console.log(id);
// set `localStorage` item key to `documentname`
// set value to `id`
localStorage.setItem(documentname, id);
});
}
};
使这项工作流的过程应该更像是:
因此,javascript只会向您的服务器发出一个请求,并返回数据,而不知道或不在乎php是如何找到它的
jQuery具有$ .post()函数, https: //api.jquery.com/jquery.post/
$.post(url, { id: 123456 });
要么
$.ajax({
method: 'POST',
url: 'http://example.api.domain.net/document.php',
data: { id: 123456 },
success: function(responseData) {
}
});
然后,PHP将能够像这样读取它:
$post_data = $_POST['id'];
// Here you do whatever with it, for example store to database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.