0

I have the following javascript code. Note the two lines which have all caps comments on them.

For some reason, in the first ajax call, if I mention at all the returned variable 'racewon' it stops the code from executing the first option (ie when 'selected' equals 'current'. When I remove these references to 'racewon' the code here works. Can anyone help?

function showe() {

    //first make everything invisible again
    document.getElementById('current').style.display = 'none';
    document.getElementById('details').style.display = 'none';
    document.getElementById('new').style.display = 'none';
    document.getElementById('delete').style.display = 'none';
    // first we take the ID of the person and pass it to our form
    var hope = $("#person").val();

    var freedome = $.ajax({
        method: "GET",
        url: "LockInPerson.php",
        data: {
            thing: hope
        }
    });
    freedome.done(function (msg) {
        var racewon = msg["winner"];
        var NoOtherAdmin = msg["NoOtherAdmin"];
    });


    var selected = $("#selector").val();
    alert(racewon); //HOW COME WHEN I MENTION THIS RETURNED VARIABLE IT FAILS

    if (selected == "current" && racewon != null) { //ALSO IT FAILS WHEN I MENTION THIS VARIABLE HERE
        document.getElementById('current').style.display = 'block';
        return;
    }

    if (selected == "details") {
        document.getElementById('details').style.display = 'block';
        var freedom = $.ajax({
            url: "RESPONDERdetails.php",
            type: "GET",
            dataType: "json",
            data: {
                thing: hope
            }
        });
        freedom.done(function (msg) {
            name = msg["username"];
            password = msg["password"];
            gender = msg["gender"];
            weight = msg["weight"];
            $("#DetName").attr("value", name);
            $("#DetPassword").attr("value", password);
            $("#DetGender").attr("value", gender);
            $("#DetWeight").val(weight);
        });
        return;
    }

    if (selected == "new") {
        document.getElementById('new').style.display = 'block';
        return;
    }

    if (selected == "delete" && NoOtherAdmin == null) {
        // this means there is another admin
        document.getElementById('delete').style.display = 'block';
        //$( "#deleter" ).submit();
        return;
    }

    if (selected == "delete" && NoOtherAdmin != null) {
        document.getElementById('makeadmin').style.display = 'block';
        return;
    }
}
laruiss
  • 3,780
  • 1
  • 18
  • 29
John
  • 45
  • 4
  • 4
    Do you understand that Ajax calls are asynchronous? You order a pizza and you try to eat it before it is delivered. And also when you define a variable inside of a block, you can not access it outside of that block. – epascarello Sep 08 '15 at 12:12
  • 1
    Put the code that uses `racewon` or `NoOtherAdmin` inside the `freedome.done` function. – Wouter Florijn Sep 08 '15 at 12:15

0 Answers0