0
// Suggested OFI Submit
$("#submit_suggested_ofi").click(function() {
    var names = $("#sofi_name").val();
    var item = $("#itemid").val();
    var item_type = $("#item_type option:selected").val();
    var notable = new Array();
    $(".notable:checked").each(function() {
        notable.push($(this).attr("id"));
    });
    notable = JSON.stringify(notable);
    var notes = $("#ofi_notes").val();
    var reading = $("#reading_notes").val();
    var aid = $("#auditor").text();
    var name_array = split(names);
    var name;
    for ( var i = 0; i < name_array.length; i++ ) {
        name = name_array[i];
        if (name && item && notes && name.trim().length > 0) {
            var query = "func=check_name&name=" + name;
            console.log("NAME: " + name);
            ajaxRequest(query, function(data) {
                console.log("NAME: " + name);
                data = data.replace(/(\r\n|\n|\r|\s)/gm, "");
                if (data == 1) {
                    if (/^\d+/.test(item)) {
                        var query = "func=submit_sofi&name=" + name + "&item=" + item + "&subtype=" + item_type + "&tags=" + notable + 
                                    "&notes=" + notes + "&reading=" + reading + "&aid=" + aid;
                        console.log(query);
                        ajaxRequest(query, function(data) {
                            successMsg(_("Suggestion Submitted"));
                            if ( i == name_array.length ) {
                                $("#sofi_name, #itemid, #ofi_notes, #reading_notes").val('');
                                $(".notable").prop('checked', false);
                            }
                        });
                    } else {
                        failureMsg(_("Item ID must be numeric"));
                    }  
                } else {   
                    failureMsg(_("Please enter a valid employee"));
                }
            });
        } else {
            failureMsg(_("Please fill out the required fields."));
        }  
    }  
});

The name variable appears to be losing its value after the first call of ajaxRequest as shown below by my console output:

NAME: Sean Garrett 
NAME: 
func=submit_sofi&name=&item=165151&subtype=chat&tags=["Policy Violation Issue"]&notes=dfgdffg&reading=dfdfdf&aid=3042

What am I doing wrong?

function ajaxRequest(query, callback) {
    $.ajax({
        type: "POST",
        url: "process/process.php",
        data: query,
        success: callback,
    });
}
morissette
  • 1,071
  • 1
  • 8
  • 29
  • 1
    Your for loop doesn't wait for the ajax request to complete before moving on to the next iteration (which changes the value of `name`) – Kevin B Apr 22 '14 at 17:40
  • added ajaxRequest function in body above, doesn't success wait for the request to complete. – morissette Apr 22 '14 at 17:42
  • 1
    @morissette success does, yes, but the for loop doesn't and your name variable is defined in the scope outside of the for loop, therefore by the time the first success callback happens, `name` has already been redefined as something else. – Kevin B Apr 22 '14 at 17:58

1 Answers1

0

You have var name; inside a function () { }. This means it is a local variable to that scope, and will be lost when the function is done, and reset anew when the function is entered again.

You also have $("#sofi_name, #itemid, #ofi_notes, #reading_notes").val(''); which will clear out the #sofi_name field. When the function starts it pulls names with var names = $("#sofi_name").val();

So perhaps you are clearing out the #sofi_name field, and thus name is getting empty the second time around.

Michael Butler
  • 6,079
  • 3
  • 38
  • 46