Click here to Skip to main content
14,932,238 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Problem while getting SPAN value on form submit after ajax call ?

It shows me when i call alert or confirm box............

C#
function chkValidDetails() {
    var isRet = "0";

    $.ajax({
                    url: '@Url.Action("GetCourseEligibility", "Master")',
                    data: { mapid: midd, challaNo: challod },
                    dataType: 'json',
                    success: function (data) {
                        debugger;
                        //alert("testug");
                        $.each(data, function (key, value) {
                            debugger;
                            if (value != null) {
                                if (catid == "3" || catid == "4") {
                                    if (value.MinResvMarksPercent != null) {
                                        if (value.MinResvMarksPercent != "" && value.MinResvMarksPercent != "null") {
                                            if (perc < parseFloat(value.MinResvMarksPercent)) {
                                                $("#errug").text("You do not have minimum required percentage " + value.MinResvMarksPercent + " for selected UG eligibility.");
                                                //isRet = "1";
                                                $("#chkmarks").val("1");
                                                $("#errdtlall").val("You do not have minimum required percentage " + value.MinResvMarksPercent + " for selected UG eligibility.");
                                                //errugajx.text("You do not have minimum required percentage " + value.MinResvMarksPercent + " for selected UG eligibility.");
                                            }
                                        }
                                    }
                                }
                                else {
                                    if (value.MinGenMarksPercent != null) {
                                        if (value.MinGenMarksPercent != "" && value.MinGenMarksPercent != "null") {
                                            if (perc < parseFloat(value.MinGenMarksPercent)) {
                                                $("#errug").text("You do not have minimum required percentage " + value.MinGenMarksPercent + " for selected UG eligibility.");
                                                //isRet = "1";
                                                $("#chkmarks").val("1");
                                                $("#errdtlall").val("You do not have minimum required percentage " + value.MinGenMarksPercent + " for selected UG eligibility.");
                                                //errugajx.text("You do not have minimum required percentage " + value.MinGenMarksPercent + " for selected UG eligibility.");
                                            }
                                        }
                                    }
                                }
                            }
                        });
                    },
                    error: function () {
                        debugger;
                        $("#errug").text("Details not found. Please relogin to save details");
                        isRet = "1";
                        $("#chkmarks").val("1");
                        $("#errdtlall").val("Details not found. Please relogin to save details");
                        //errugajx.text("Details not found. Please relogin to save details");
                    }
                });

                if ($("#chkmarks").val() == "1") {
                    return "0";
                }
}


i call above function on form submit i assign value in any span errugajx, $("#errdtlall"), $("#errug"). it does not take it and it remains blank untill i call alert or confirm.


I checked, Problem is very strange.. When i debug then i found that after calling above function i called confirm box... but confirm box comes first before completion of ajax request... and when i press ok. then it goes back to ajax call and throws into error part of ajax.... thats why it does not able to check span value correctly. So one more problem is confirm box fired first before ajax completion.... And if i do not put confirm or alert after the function...so it does not show error msg and submits the form to server.....
Posted
Updated 13-Apr-14 23:40pm
v5
Comments
Sampath Lokuge 14-Apr-14 4:32am
   
Where's your code snippet ?
Gaurav Gupta A Tech Guy 14-Apr-14 4:36am
   
function chkValidDetails() {
var isRet = "0";

$.ajax({
url: '@Url.Action("GetCourseEligibility", "Master")',
data: { mapid: midd, challaNo: challod },
dataType: 'json',
success: function (data) {
debugger;
//alert("testug");
$.each(data, function (key, value) {
debugger;
if (value != null) {
if (catid == "3" || catid == "4") {
if (value.MinResvMarksPercent != null) {
if (value.MinResvMarksPercent != "" && value.MinResvMarksPercent != "null") {
if (perc < parseFloat(value.MinResvMarksPercent)) {
$("#errug").text("You do not have minimum required percentage " + value.MinResvMarksPercent + " for selected UG eligibility.");
//isRet = "1";
$("#chkmarks").val("1");
$("#errdtlall").val("You do not have minimum required percentage " + value.MinResvMarksPercent + " for selected UG eligibility.");
//errugajx.text("You do not have minimum required percentage " + value.MinResvMarksPercent + " for selected UG eligibility.");
}
}
}
}
else {
if (value.MinGenMarksPercent != null) {
if (value.MinGenMarksPercent != "" && value.MinGenMarksPercent != "null") {
if (perc < parseFloat(value.MinGenMarksPercent)) {
$("#errug").text("You do not have minimum required percentage " + value.MinGenMarksPercent + " for selected UG eligibility.");
//isRet = "1";
$("#chkmarks").val("1");
$("#errdtlall").val("You do not have minimum required percentage " + value.MinGenMarksPercent + " for selected UG eligibility.");
//errugajx.text("You do not have minimum required percentage " + value.MinGenMarksPercent + " for selected UG eligibility.");
}
}
}
}
}
});
},
error: function () {
debugger;
$("#errug").text("Details not found. Please relogin to save details");
isRet = "1";
$("#chkmarks").val("1");
$("#errdtlall").val("Details not found. Please relogin to save details");
//errugajx.text("Details not found. Please relogin to save details");
}
});

if ($("#chkmarks").val() == "1") {
return "0";
}
}


i call above function on form submit i assign value in any span errugajx, $("#errdtlall"), $("#errug"). it does not take it and it remains blank untill i call alert or confirm.
Sampath Lokuge 14-Apr-14 4:41am
   
Check whether are there any errors on js file by using chrome dev tools ?
Gaurav Gupta A Tech Guy 14-Apr-14 5:03am
   
I checked, Problem is very strange.. When i debug then i found that after calling above function i called confirm box... but confirm box comes first before completion of ajax request... and when i press ok. then it goes back to ajax call and throws into error part of ajax.... thats why it does not able to check span value correctly. So one more problem is confirm box fired first before ajax completion....

And if i do not put confirm or alert after the function...so it does not show error msg and submits the form to server.....
Nelek 14-Apr-14 4:51am
   
Please, get used to add the code to the question, it is easier for the rest to follow up and will help you getting the answers
Gaurav Gupta A Tech Guy 14-Apr-14 5:06am
   
ok...thank you for your suggestion..Plz help me find out above problem..

There is no strange result here.You're trying to check the async result inside the sync method.B'cos of that, it always fires sync method before the async method.

So you have 2 options here.First one is, check your SPAN value inside the ajax (async) method.Or second method is make your ajax method as sync by using
async:false

Please try that and let me know,if you have any issues.
   
Comments
Gaurav Gupta A Tech Guy 14-Apr-14 7:08am
   
Whether sync method will be supported by all browsers ?
Gaurav Gupta A Tech Guy 14-Apr-14 7:36am
   
Yeah it is working now.....Thank you. But it will work on all browser or not for sync ?
Sampath Lokuge 14-Apr-14 7:43am
   
It's working for all browsers.No problem at all. :)
Async or sync is not a browser dependent behaviour.It depends on the way you call or request the data from the server.That's it.
Gaurav Gupta A Tech Guy 14-Apr-14 8:19am
   
Yeah..... I know that...but i read one of blog on internet saying that sync behavior do not supported by all browser..........i know that it can not but have created confusion that's why i asked.
please put this line of your ajax request before success: ...

async: false,
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900