napsternapster Posted April 20, 2009 Share Posted April 20, 2009 how does javascript choose which statement to process from a function? //javascript function called onclick of a button function process(n) { var selection = document.getElementById("sel_id").value; var official_name = document.getElementById("txtOff_name").value; var urlName="checkName.php"; urlName=urlName+"?q="+official_name; urlName = urlName+"&sid="+Math.random(); ShowErrors(urlName,"GET","txtOff_name","lblOff_error",n); //to output the results expandANDcallapse(n); } //ajax function function ShowErrors(url,method,id,label,n) { var xmlHttp = new GetXmlHttpObject(); if(method == "GET") { //alert(method); xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange= function() { if(xmlHttp.readyState== 4 && xmlHttp.status==200) { if(xmlHttp.responseText != 0) { server_error_counter++; document.getElementById(id).style.background = "red"; document.getElementById(label).innerHTML = xmlHttp.responseText; } else { document.getElementById(id).style.background = "orange"; document.getElementById(label).style.background = ""; } } } xmlHttp.send(null); } } function expandANDcallapse(n) { //alert(string); if(n != 0) { expand(n); } alert(server_error_counter); } //////////////////// can anyone tell me why javascript first processes the function call expandANDcallapse(n); before the function ShowErrors completes processing. Quote Link to comment Share on other sites More sharing options...
Axeia Posted April 20, 2009 Share Posted April 20, 2009 //////////////////// can anyone tell me why javascript first processes the function call expandANDcallapse(n); before the function ShowErrors completes processing. Because ajax is Asynchronous! If it wasn't your browser would pretty much hang while the page is being requested, code below is probably what you want to do. //javascript function called onclick of a button function process(n) { var selection = document.getElementById("sel_id").value; var official_name = document.getElementById("txtOff_name").value; var urlName="checkName.php"; urlName=urlName+"?q="+official_name; urlName = urlName+"&sid="+Math.random(); ShowErrors(urlName,"GET","txtOff_name","lblOff_error",n); //to output the results //Function call should not be here. } //ajax function function ShowErrors(url,method,id,label,n) { var xmlHttp = new GetXmlHttpObject(); if(method == "GET") { //alert(method); xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange= function() { if(xmlHttp.readyState== 4 && xmlHttp.status==200) { if(xmlHttp.responseText != 0) { server_error_counter++; document.getElementById(id).style.background = "red"; document.getElementById(label).innerHTML = xmlHttp.responseText; } else { document.getElementById(id).style.background = "orange"; document.getElementById(label).style.background = ""; expandANDcallapse(n); //<- Should be here! } } } xmlHttp.send(null); } } function expandANDcallapse(n) { //alert(string); if(n != 0) { expand(n); } alert(server_error_counter); } Quote Link to comment Share on other sites More sharing options...
napsternapster Posted April 21, 2009 Author Share Posted April 21, 2009 both selection and official_name must be validated using the same function(ShowErrors) and make sure no errors are found between the 2.a nested for loop is created reading data from the dymicaly created textboxes and dropdown list validated using the same function ShowErrors before outputing any results to the user. function process(n) { var selection = document.getElementById("sel_id").value; var official_name = document.getElementById("txtOff_name").value; var urlselection ="entity_type.php"; urlselection=urlselection+"?type="+selection ; urlselection= urlselection+"&sid="+Math.random(); ShowErrors(urlselection,"GET","selection","lblentity_error",n); var urlName="checkName.php"; urlName=urlName+"?q="+official_name; urlName = urlName+"&sid="+Math.random(); ShowErrors(urlName,"GET","txtOff_name","lblOff_error",n); for(var counter_size = 1; counter_size <= contact_counter; counter_size++) //contact_counter { for(var count_size = 1; count_size <= detailed_counter; count_size++) //detailed_counter { for(var ct_size = 1; ct_size <= 9; ct_size++) { if(counter_size == 1) { //getting the id of the select entity and information var infor_id = document.getElementById("txt"+counter_size+"_"+count_size+"_"+ct_size).id; var select_id = document.getElementById("select_id"+counter_size+"_"+count_size+"_"+ct_size).id; //getting the value of the select entity and information var infor = document.getElementById(infor_id).value; var selected_info = document.getElementById(select_id).value; //finding the id and value of the hidden field from php var detailed_infor_types_id = document.getElementById("txt"+count_size).id; var detailed_infor_types_value = document.getElementById(detailed_infor_types_id).value; if((document.getElementById(infor_id).id != null && document.getElementById(select_id).id != null) || (document.getElementById(infor_id).value != "" && document.getElementById(select_id).value !="")) { if(infor != "") { var label = null; //alert(count_size); var urlContacts = "contact_numbers.php"; urlContacts = urlContacts+"?contact="+infor+"&type="+detailed_infor_types_value; urlContacts=urlContacts+"&sid="+Math.random(); //alert("detailed_counter: "+detailed_counter); executeAjax(urlContacts,"GET",infor_id,label,f,count_size,detailed_counter) } } } } } } //to output the results //Function call should not be here. } //ajax function function ShowErrors(url,method,id,label,n) { var xmlHttp = new GetXmlHttpObject(); if(method == "GET") { //alert(method); xmlHttp.open("GET",url,true); xmlHttp.onreadystatechange= function() { if(xmlHttp.readyState== 4 && xmlHttp.status==200) { if(xmlHttp.responseText != 0) { server_error_counter++; document.getElementById(id).style.background = "red"; document.getElementById(label).innerHTML = xmlHttp.responseText; } else { document.getElementById(id).style.background = "orange"; document.getElementById(label).style.background = ""; expandANDcallapse(n); //<- Should be here! } } } xmlHttp.send(null); } } function expandANDcallapse(n) { //alert(string); if(n != 0) { expand(n); } alert(server_error_counter); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.