1

I have code in that javascript and ajax call need to be work simultaneously. But it did not work. And always go to return true.

Functionality of create function is validation of data and ajax call which execute query and depend on result it will further execute. If response text is yes then it call confirmval function. And then further it ask for confirmation and next execution But I face problem is function not return false it always go to true. I cant understand why this happening?

function create()
            {                        
                if (document.companyregister.cmpname.value === "")
                {
                    alert("Please Enter Company name");
                    document.companyregister.cmpname.value = "";
                    document.companyregister.cmpname.focus();
                    return false;
                }

                var companyname = document.companyregister.cmpname.value;
                var username = document.companyregister.username.value;
                    $.ajax({
                    url: 'checkexisting.php',
                    type: 'POST',
                    data: {companyname: companyname,username:username},
                    success: function(errorResponse) {
                        var result = errorResponse.trim();                        
                        if(result=="yes"){                            
                            return confirmval(companyname,username);                                
                        }
                        else{
                            document.getElementById("formsubmitting").style.display = "block";
                            document.getElementById("hidesubmit").style.display = "none";
                            return true;
                        }                        
                        }
                    });
}

function confirmval(companyname,username){
                var c = confirm("This company is already created.\nWould you like to delete existing company?");                
                if(c){  
                    alert("c");                    
                    $.ajax({
                    url: 'updatecompany.php',
                    type: 'POST',
                    data: {companyname: companyname,username:username},
                    success: function(responsetext) {
                        var result = responsetext.trim();                        
                        if(result=="yes"){
                            document.getElementById("formsubmitting").style.display = "block";
                            document.getElementById("hidesubmit").style.display = "none";
                            return true;
                        }                                            
                        }
                    });
                }
                else{
                    alert("notc");                    
                    window.location="http://www.google.com";
                }
            }
LOKESH
  • 1,303
  • 1
  • 16
  • 29

1 Answers1

-1

You are trying to return two values after your first ajax call:

if(result=="yes"){                            
    return confirmval(companyname,username);
    return false;
}

This will just return the result of confirmval function (which appears to always return true), and the 'return false' line will never be run, thus it will always return true.

If you need that result=="yes" to return false, I might recommend something like:

if(result=="yes"){
    var confirmvalResult = confirmval(companyname,username);
    if(confirmvalResult) {
        return false;
    } else {
        // not sure what you want to do here
    }
}
Brendan D.
  • 96
  • 7