-1

I have javascript function that includes few if else statements for some reason one of the statements is not working properly.

Script:

function makePayment(reservationID) {
var expirationDate = $("#expirationDate").val();
var creditCard = $("#creditcard").val();

if (creditCard.length == 16 && expirationDate.length == 4) {
    var month = "";
    var year = "";
    month += expirationDate.charAt(0);
    month += expirationDate.charAt(1);

    year += expirationDate.charAt(2);
    year += expirationDate.charAt(3);

    var monthInt = parseFloat(month);
    var yearInt = parseFloat(year);
    var currect;

    if (monthInt > 0 && monthInt < 13 && yearInt > 12 && yearInt < 24) {



        $.ajax({
            url: "CreditAuthentication",
            data: "type=reserve&creditCard=" + creditCard + "&expirationDate=" + expirationDate,
            success: function(responseText) {
                currect = responseText;
                console.log(responseText);
            }, error: function(xhr) {
                alert(xhr.status);
            }});

        if (currect == true) {

            //
            $.ajax({
                type: "POST",
                url: "paymentMethods",
                data: "type=make&reservationID=" + reservationID,
                success: function(msg) {
                    console.log("Paymentmade");
                    alert("Payment made");

                    //alert(CCA);
                    document.location = "index.jsp#reservation";
                }
            });
        } 

        else if(currect === "false") {
             alert("Please enter valid details. 3rd");
        }
        //
    } else {
        alert("Please enter valid details. 2nd");
    }

} else {
    alert("Please enter valid payment information. 1st");
}
}

I have checked in FireBug and the return value from my servlet is correct and and the console.log(responseText); displays the response in console. However for some reason this part of statement is not working and not displaying the alerts:

if (currect == true) {

    $.ajax({
        type: "POST",
        url: "paymentMethods",
        data: "type=make&reservationID=" + reservationID,
        success: function (msg) {
            console.log("Paymentmade");
            alert("Payment made");
            document.location = "index.jsp#reservation";
        }
    });

} else if (currect === "false") {
    alert("Please enter valid details. 3rd");
} 

Thanks in Advance!

palaѕн
  • 72,112
  • 17
  • 116
  • 136
Poorya
  • 1,291
  • 6
  • 27
  • 57
  • 2
    Possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Frédéric Hamidi Nov 11 '13 at 07:44
  • With AJAX, `below` doesn't mean `after`. Variable `currect` is checked _before_ AJAX callback has any chance to assign the result to it. – raina77ow Nov 11 '13 at 07:45
  • 1
    Why if currect == true ?? and the else if Just do if(currect) {} else{} – Ahmad Nov 11 '13 at 07:45
  • 1
    why do you compare currect with String "false"? Isn't it a boolean? – hgoebl Nov 11 '13 at 07:47
  • @raina77ow so a While(currect !=Null) might be helpful ? or is there a better way? – Poorya Nov 11 '13 at 07:48
  • @pouria No, it won't be. Check the question linked to this as its duplicate; it has all the relevant info. – raina77ow Nov 11 '13 at 07:49
  • Yes. It's well known that unfortunately sometimes the `if` statement in Javascript doesn't work properly. You should file a bug report to netscape (and yes... those hoof prints are for sure zebras). – 6502 Nov 11 '13 at 07:58

2 Answers2

0

Please make sure currect is not null or undefined and if it is not. make a 'currect' global so it can be accessable:

  var currect; //declare it here
  $.ajax({
            url: "CreditAuthentication",
            data: "type=reserve&creditCard=" + creditCard + "&expirationDate=" + expirationDate,
            success: function(responseText) {
                currect = responseText;
                console.log(responseText);
            }, error: function(xhr) {
                alert(xhr.status);
            }});

then if currect is a boolean then try:

if (currect) {
   //your code
} else {
   //your code
} 

and if it is string:

if (currect === 'true') {
   //your code
} else if(currect === 'false') {
   //your code
}
Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
0

You have to place the currect if/else in the success function of your ajax call.

hgoebl
  • 12,637
  • 9
  • 49
  • 72