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 Answer 1

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

Sign up to request clarification or add additional context in comments.

11 Comments

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.
You can put this code anywhere in the scope of the function. Function definitions are hoisted, so the order doesn't matter.
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.
There's nothing in the code you've shown that would cause it to loop.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.