0

I don't know how to put the value of this.responseText into index_no, it's like the function into the onreadystatechange event it's creating its own variable called inde_no, 'cause if I do an alert out of the function it print undefined

let index_no;
function segnalibro(){

    var xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        index_no=parseInt(this.responseText);
    }
}
    xmlhttp.open("GET", "Segnalibro.php?ISBN="+ISBN, true);
    xmlhttp.send();
}
segnalibro();
  • "if I do an alert out of the function it print undefined" — Because you're alerting **before** the ready state has changed and triggered the function to update the value of the variable. – Quentin May 20 '21 at 10:56
  • thanks @Quentin that link was very useful – Pietro Putzolu May 20 '21 at 12:05

0 Answers0