0

I am trying to find out if a text file (note) exists on the server. I want to return "yes" if it does, "no" if it does not. I am able to get this into an alert successfully (for each of the 7 occurrences) but I want to return it back into the originating html doc so I can use it in future code. I am reading about callbacks on this site, but not sure how to implement it.

I tried adding some callback code to the success function, that I saw in an example elsewhere here but am unsure how to edit my function call:

tmpNoteFileSun is a text string that matches the format of the text files stored on the server.

The function call (there are 7 of these in separate places, 1 for each day of the week):

CheckNoteExist(tmpNoteFileSun);
var DoesTheNoteExist = ""; //code needs to go here that returns noteexists (as in the alert below).

I tried changing the above to:

var DoesTheNoteExist = CheckNoteExist(tmpNoteFileSun);
console.log("Does Note Exist " + DoesTheNoteExist);

But get undefined in the console.

The Ajax Function:

function CheckNoteExist(ThisNoteName, callback) {

var NoteFileName = ThisNoteName;

    // Ajax to call an external php file, pass the notes filename to it and check if the file
    // exists. If it does, change noteexists variable to Yes", else it is "no".
    $.ajax({
        url: 'ajaxfile_note_exists.php',
        type: 'GET',
        data: {NoteFileName: NoteFileName},
        success: function(noteexists) {
          alert("Does the note exist: " + noteexists);
          callback && callback(noteexists);
        }
    });

}

The external PHP file:

$filename = "upload/" . $_GET['NoteFileName'];

if (file_exists($filename)) {
  $noteexists = "yes";
}
else {
  $noteexists = "no";
}

echo $noteexists;

?>

1 Answers1

1

You're not using the callback, that's what it's there for.

CheckNoteExist(ThisNoteName, val => console.log("Does Not Exist " + val));

See also How do I return the response from an asynchronous call? and Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks, I am trying to find where to put the code. After the Ajax code? Also had to remove the trailing s from the function name. Reading the link you provided now. –  Sep 06 '19 at 21:15
  • You can put this code anywhere in the scope of the function. Function definitions are hoisted, so the order doesn't matter. – Barmar Sep 06 '19 at 21:25
  • I have tried it in a few places and it keeps looping the alert. Have to kill the browser session to stop it. Elsewhere I am getting Maximum call stack size exceeded RangeError: Maximum call stack. For sure i am doing something wrong. will keep trying. –  Sep 06 '19 at 21:33
  • There's nothing in the code you've shown that would cause it to loop. – Barmar Sep 06 '19 at 21:37
  • I can find nothing that causes a loop. Not sure what the problem is. I think I am just going to write the code out for each day with a separate php file for each and store each value in a separate local storage. –  Sep 06 '19 at 22:27
  • Maybe it has something to do with the way you're calling it for each day. If you post the full code, maybe I can see it. – Barmar Sep 06 '19 at 22:37
  • I commented out the other 6 function calls and still get the loop. There is nothing in the code. it just gets a cookie value, Takes the first 10 characters and turns it into a string and appends .txt to it. I am looking at the original code I read about callbacks and seeing if I can implement that. If not I will go the long route and code each day separately. –  Sep 06 '19 at 22:45
  • https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call/14220323#14220323 - this looks promising so I will try this. –  Sep 06 '19 at 22:47
  • That's why I linked to it in the answer. – Barmar Sep 06 '19 at 22:48
  • None of that really helped me. All goes over my head. Spent all night trying to get one of them to work without success. Tired to convert the one one here without success: https://stackoverflow.com/questions/14200021/passing-a-callback-function-with-included-parameters/14200148#14200148 And the looping reason. Could this be because I call the Ajax function on page load, in $(document).ready(function() {? –  Sep 07 '19 at 08:20
  • I have temporarily set async: false and added the value to localstorage while I figure this out. It is all working flawlessly with no perceptible speed decrease. Will revisit this in a couple of days when I work on next version of the app. –  Sep 08 '19 at 12:25