0

I have a JSP with the form "deleteCont" and a javascript file with the function "validateDelete". When I click the "Delete contact" button the "validateDelete" function is successfully called and the alert with the "alertmessage" is displayed. However, the "ContactMaint" servlet is still called even though false is being returned. Please can someone tell me where I am going wrong?

<form name="deleteCont"
    onsubmit="return validateDelete('${sessionScope.account}','${sessionScope.firstname}', '${sessionScope.lastname}')" 
    action="ContactMaint" method="post">
    <input type="submit" value="Delete contact" name="delContact">
</form>   



function validateDelete(account, contactFirstName, contactLastName) {
    urlstrg = "http://localhost:8080/SalesPoliticalMapping/JCheckContacts?account=" +
        account + "&firstname=" + contactFirstName + "&lastname=" + contactLastName;
    $.get(urlstrg, function (result, status) {
        if (result.length > 0) {
            var alertmessage = "Can't delect contact:";
            for (var i = 0; i < result.length; i++) {
                alertmessage += result[i];
            }
            alert(alertmessage);
            return false;
        }
    });
}

I have now amended my code as follows although I'm not entirely sure why:

function validateDelete(account, contactFirstName, contactLastName) {
$.ajaxSetup({async: false});
urlstrg = "http://localhost:8080/SalesPoliticalMapping/JCheckContacts?account=" +  account + "&firstname=" + contactFirstName + "&lastname=" + contactLastName;

var valid = true;
var alertmessage = "";

$.get(urlstrg, function(result, status) {
   if (result.length > 0) {
   valid = false;    
   alertmessage = "Can't delect contact:";
   for (var i = 0; i < result.length; i++) {
       alertmessage += result[i];
        }
   }
 });

 if (valid===false) {
     alert(alertmessage);
 return false;}
 };
1
  • 2
    return false should be outside $.get() [in the scope of validateDelete]. Commented Aug 18, 2014 at 20:21

1 Answer 1

2

There are 2 problems:

  1. You're firing an ajax request. Ajax requests are by default asynchronous. Immediately after the $.get() called, the code advances to the next line while the asynchronous request runs in the background in a completely different thread. As there's in your case apparently nothing after the $.get(), it immediately returns (implicitly as true).

  2. You're returning from a callback function, not from the main function. The callback function only returns into the main function, not outside. You need to return from the main function.

One way is to always return false and explicitly submit the form on success.

function validateDelete(...) {
    var $form = $(this);

    $.get(..., function(...) {

        if (success) {
            $form.submit();
        }
    });

    return false;
}

Another way is making the ajax request asynchronous by passing { async: false } option as 2nd argument of $.get().

function validateDelete(...) {
    var valid = false;

    $.get(..., { async: true }, function(...) {

        if (success) {
            valid = true;
        }
    });

    return valid;
}

Please note that this problem has completely nothing to do with JSP. You'd still have had exactly the same problem when using the same script in another view technology, such as PHP, ASP, etc, and even in a plain HTML page. JSP is in the context of this question merely a HTML code generator and can better be omitted from the question in future problems with specifically HTML+CSS+JS/jQuery.

Sign up to request clarification or add additional context in comments.

3 Comments

I have a $.ajaxSetup({async: false}); in another function which executes earlier but I have added another just before the $.get in function validateDelete. The result is still the same. The alert always gets displayed so I don't believe the problem is an asynchronous issue.
Code now working (see edited question). Not sure why?
Indeed, you was only returning in the callback function, not in the main function. I'll update the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.