So my problem is actually pretty simple:
I have this function (simplified) - it is triggered when a button is clicked:
$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);
});
};
What I want to do is:
After findid();
is executed and id
has a value I want to save this value as a readable file on the server. It's filename should be the same as the name of the document it's coming from (in this case John
). The content of the file should just be the value of id
(in this case 123456
). Which file format? I don't know. And here is the next problem...
Reuse the file:
The next thing I wish I would be able to do is, to load this generated file when the exact documentname was inputted again by another user. Because if the file already exists, it would be unnecessary to load the whole JSON again. So the updated code would look like this (i know that this isn't actual code, but maybe it's easier to understand with this):
$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]
});
}
};
I already tried a lot of things with Ajax and jQuery's
$.get
, I just have no idea how to handle sent things with PHP. I don't know which file format is best for this, or if this is even possible. I don't know how to transfer variables from JavaScript to PHP documents or how to use them there or vice versa.
PS: My developer environment: I don't run a server by myself, I have some webspace and a domain from a web service. Yes it supports PHP. MySQL and other database types are available too, if that's a useful information. jQuery is also installed.
Sorry again, for my bad english and for my lack of knowledge and interest in PHP and other serverside things. However, I hope you can help me somehow.