繁体   English   中英

如何在使用PHP的服务器上保存JavaScript字符串并稍后再次请求?

[英]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]
        });
    }
};


我已经使用Ajax和jQuery的$.get尝试了很多事情,但我不知道如何使用PHP处理已发送的事情。 我不知道哪种文件格式最适合此操作,或者甚至是否可能。 我不知道如何将变量从JavaScript传输到PHP文档,或者在那里如何使用它们,反之亦然。
PS:我的开发人员环境:我自己没有运行服务器,我有一些Web空间和Web服务中的域。 是的,它支持PHP。 如果这是有用的信息,则也可以使用MySQL和其他数据库类型。 jQuery也已安装。
再次抱歉,我的英语不好,也缺乏对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文件

使这项工作流的过程应该更像是:

  1. ajax请求到服务器上的php端点。
  2. php检查数据是否存储在本地(文件或数据库中)
  3. 如果尚未存储,则php向其他api发出请求。 当api返回数据时,php将其存储在文件或数据库中。
  4. php从本地源或api响应将数据返回给ajax。

因此,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM