无法在JavaScript中获取方法之外的值

时间:2017-12-30 11:19:01

标签: javascript

这里我试图通过JavaScript通过JavaScript验证它将一些值传递给PHP但是我没有在databaseEntry()方法中获得getcanID()方法的值can_id,尽管它是全局的。事实上,我没有得到xhttp.onreadystatechange之外的值现在我怎样才能在databaseEntry()方法中获得该值? 这里是JS的代码片段

    var signFlag = 0;

function Checker() {

    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        console.log(this.readyState);

        if(this.readyState == 4 && this.status == 200) {
            console.log(this.status);
            var response = xhttp.responseText;
            //alert(response);
            if( String(response.trim()) === "yes" ){

                checkName();
                checkGender();
                checkMailID();

                checkDOB();
                checkAddress();
                checkReligion();
                checkContact();
                checkWorkexp();
                checkExam();
                checkBoard();
                checkGroup();
                checkPassYear();
                checkResult();
                checkSub();
                checkUniv();
                getcanID();
                console.log("candidate_id"+can_id);
                databaseEntry();
            }
            else{
                //document.getElementById('navBarLog').style.display='none';
                alert("Log in First! :)");
                window.location.href = "index.html";
                $('#id1').modal('hide');
            }
        }

    }
    xhttp.open("GET", "checkSession.php", true);
    xhttp.send();

}
/*
.
.
.
*/
function getcanID(){
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
        console.log(this.readyState);

        if(this.readyState == 4 && this.status == 200) {
            console.log(this.status);
            var response = xhttp.responseText;
            //alert(response);
            can_id = response;
            console.log("inside method"+can_id);
        }

    }
    xhttp.open("GET","getcanID.php", true);
    xhttp.send();

}

function databaseEntry() {


    if(signFlag==1) {
        console.log("fill up correctly!");
        alert("fill up correctly!");
    }
    else
    {
        console.log(can_id);
        console.log(name);
        console.log(mail);
        console.log(result);
        console.log(result1);
        console.log(result2);
        console.log(result3);
        console.log(group);
        console.log(group1);
        console.log(wexp);


        var submitError = document.getElementById("subErr");
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            console.log(this.readyState);
            if(this.readyState == 4 && this.status == 200)
            {
                console.log(this.status);
                var response = xhttp.responseText;
                console.log(response);
                submitError.innerHTML=response;
                alert(response);
                if(String(response.trim()) === "Success") {
                    alert("Successfully submitted :)");
                    window.location.href = "index.html";
                }
                else
                    alert(response);
            }
        }

        xhttp.open("GET","form.php?can_id="+can_id+"&name="+name+"&mail="+mail+"&gender="+gender+"&dob="+dob+"&address="+address+"&religion="+religion+"&contact="+contact+"&wexp="+wexp+"&exam="+exam +"&exam1="+exam1+"&exam2="+exam2+"&exam3="+exam3+"&board="+board+"&board1="+board1 +"&group="+group+"&group1="+group1+"&pass="+pass+"&pass1="+pass1+"&pass2="+pass2+"&pass3="+pass3+"&result="+result +"&result1="+result1+"&result2="+result2+"&result3="+result3+"&subject="+subject+"&subject1="+subject1+"&university="+university+"&university1="+university1,true);
        xhttp.send();

    }
}

0 个答案:

没有答案