Greywacke Posted June 28, 2010 Share Posted June 28, 2010 hi there,sitting with a slight issue here. an ajax post request is sent after form validation, and i need to retrieve the latest value from the database to compare against with form validation.at the moment i just inserted an ajax request... looping till the hidden field gets the updated value, and continuing validating it. however - there is a problem with this method - if the internet is slow (bandwidth is limited for whatever reason), it hangs the browser 0ohere is the excerpt from the current script, inserted after validating the other fields: makeRequest("GET", "scripts/ajax_suppliers.php?q=9&s="+document.form_suppliers.list_suppliers.options[document.form_suppliers.list_suppliers.selectedIndex].value.split(";,;")[0], "");while (document.form_suppliers.hidden_creditbalance.value == "-1") { // wait till hidden balance is updated}var fb = parseInt(document.form_suppliers.text_creditbalance.value);var cb = parseInt(document.form_suppliers.hidden_creditbalance.value);var reason = "";if (fb != cb && cb != -1) { if (!confirm(trim(document.form_suppliers.list_suppliers.options[document.form_suppliers.list_suppliers.selectedIndex].text.split(" - ")[0]) + " has a current balance of " + cb + " PAYG Credits.\n" + "Adjust this balance to reflect " + fb + " PAYG Credits?")) { document.getElementById("ajaxbg").style.visibility = "hidden"; document.form_suppliers.text_creditbalance.focus(); return false; } else { reason = prompt("Please enter a reason for updating credits.", ""); }} here are the makeRequest and alertContents function on this page, which now includes support for just the cbal tag. function makeRequest(method, url, parameters) { var http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { // set type accordingly to anticipated content type http_request.overrideMimeType('text/xml'); //http_request.overrideMimeType('text/html'); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { salert('Cannot create XMLHTTP instance'); return false; } http_request.onreadystatechange = function() { alertContents(http_request); } url += (method=="GET")?parameters:""; http_request.open(method, url, true); if (method == "POST") { http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http_request.setRequestHeader("Content-length", parameters.length); http_request.setRequestHeader("Connection", "close"); } http_request.send((method=="GET")?null:parameters); ajaxloading++;}// xml parserfunction alertContents(http_request) { if (http_request.readyState == 4) { // filter state changed if (http_request.status == 200) { // filter errors var xmldoc = http_request.responseXML; // get response //salert(http_request.responseText); // salert xml data var cb = false; // current balance flag var stats = false; // get timeframe info var arrTF = new Array(); var rows1 = xmldoc.getElementsByTagName("timeframe"); for (var rr = 0; rr < rows1.length; rr++) { if (rows1[rr].attributes) { arrTF[0] = rows1[rr].attributes.getNamedItem("name").nodeValue; arrTF[1] = rows1[rr].attributes.getNamedItem("value").nodeValue; } } // parse records var x = 0; var arrStats = new Array(); var rows2 = xmldoc.getElementsByTagName("statistics"); for (var r = 0; r < rows2.length; r++) { var records = rows2[r].getElementsByTagName("record"); if (rows2[r].attributes) { arrStats[x] = rows2[r].attributes.getNamedItem("name").nodeValue; x++; } for (var c = 0; c < records.length; c++) { var record = records[c]; if (record.attributes) { arrStats[x] = new Array(); arrStats[x][0] = record.attributes.getNamedItem("statname").nodeValue; if (arrTF[0] != "timeframe_top10transactions") { arrStats[x][1] = record.attributes.getNamedItem("leads").nodeValue; } else { arrStats[x][1] = record.attributes.getNamedItem("amount").nodeValue; } x++; } } stats = true; } if (stats) insertstats(arrTF,arrStats); var rows = xmldoc.getElementsByTagName("supplier"); for (var r = 0; r < rows.length; r++) { var i = 0; var arr = new Array(); var aa = 0; var arr1 = new Array(); var sa = 0; var arr3 = new Array(); var lc = 0; var arr5 = new Array(); for (var c = 0; c < rows[r].childNodes.length; c++) { var supplierdetail = rows[r].childNodes[c]; if (supplierdetail.childNodes.length > 0) { arr[i] = (supplierdetail.firstChild.data==" ")?"":supplierdetail.firstChild.data; i++; } else if (supplierdetail.nodeName == "serviceattrib") { var arr2 = new Array(); for (var a = 0; a < supplierdetail.attributes.length; a++) { arr2[a] = supplierdetail.attributes[a].value; } arr1[aa] = arr2.join(";:;"); aa++; } else if (supplierdetail.nodeName == "supplierattrib") { var arr4 = new Array(); for (var s = 0; s < supplierdetail.attributes.length; s++) { arr4[s] = supplierdetail.attributes[s].value; } arr3[sa] = arr4.join(";:;"); sa++; } else if (supplierdetail.nodeName == "lcmp") { var arr6 = new Array(); for (var q = 0; q < supplierdetail.attributes.length; q++) { arr6[q] = supplierdetail.attributes[q].value; } arr5[lc] = arr6.join(";:;"); lc++; } } arr[i] = arr1.join(";.;"); // available supplier attributes i++; arr[i] = arr3.join(";.;"); // selected supplier attributes i++; arr[i] = arr5.join(";.;"); // linked leads 360 lead 360 campaigns i++; //salert(arr); if (arr) addrecord("list_suppliers", arr); } var catg = xmldoc.getElementsByTagName("pcats"); for (var g = 0; g < catg.length; g++) { var cats = catg[g].getElementsByTagName("pcat"); var n = 0; var arrpc = new Array(); for (var p = 0; p < cats.length; p++) { var cat = cats[p]; if (cat.attributes) { arrpc[n] = new Array(); for (var o = 0; o < cat.attributes.length; o++) { arrpc[n][o] = cat.attributes[o].value; } n++; } } addcategories(arrpc); } // update current balance if (xmldoc.getElementsByTagName("cbal")[0]) { if (xmldoc.getElementsByTagName("cbal")[0].firstChild) { var cbal = xmldoc.getElementsByTagName("cbal")[0].firstChild.data; if (cbal != undefined) { document.form_suppliers.hidden_creditbalance.value = cbal; cb = true; } } } // mysql queries / failures if (xmldoc.getElementsByTagName("sql")[0]) { if (xmldoc.getElementsByTagName("sql")[0].firstChild) { var sql = xmldoc.getElementsByTagName("sql")[0].firstChild.data; if (sql != undefined) { salert(sql); } } } } else { salert('There was a problem with the request.'); } if (!cb) { document.form_suppliers.list_suppliers.selectedIndex = supplier; document.form_suppliers.list_suppliers.onchange(); } ajaxloading--; sessrem = sesstot; if (ajaxloading <= 0 && !cb) document.getElementById("ajaxbg").style.visibility = "hidden"; }} the xmlrequest document sends the balance for the supplier's id in the querystring. however, if the net is slow (limited bandwidth), it hangs the browser.is there perhaps a smarter way to do this, the first script is called within the onclick event for the modify button. i cannot wait a specific amount of time and the validation and post request need to continue after the balance validation has been completed. Link to comment Share on other sites More sharing options...
wirehopper Posted June 28, 2010 Share Posted June 28, 2010 You might want to consider an existing AJAX library, such as jQuery or dojo. You'll save a lot of time. Link to comment Share on other sites More sharing options...
Greywacke Posted June 28, 2010 Author Share Posted June 28, 2010 sighs... unfortunately the ajax here needs to be developed individually. the script is working - has always been working, just how could i make the recently added script wait till the hidden fields value is not -1, then continue with script without hanging the browser (wait for an ajax request to complete before completing validation and submitting the post request) for the modify button's onclick event? Link to comment Share on other sites More sharing options...
justsomeguy Posted June 28, 2010 Share Posted June 28, 2010 Use a synchronous request instead of asynchronous. That will have the browser wait for the request to come back instead of using an infinite idle loop. You could also use an asynchronous request and have the callback function do the next steps, that's the usual way when you have a process with ajax steps in it. Link to comment Share on other sites More sharing options...
Greywacke Posted June 28, 2010 Author Share Posted June 28, 2010 thanks again justsomeguy, i was starting to think in that direction - but not exactly sure of what i needed to do. i will do these changes in the morning and reply with the results Link to comment Share on other sites More sharing options...
Greywacke Posted June 29, 2010 Author Share Posted June 29, 2010 awesome :)i have successfully moved the rest of the validation and the second ajax request to the ajax response for the first request. it works successfully without hanging the browser :)thanks a mill justsomeguy, for pointing me in the right direction :)this issue is now RESOLVED Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.