I am trying to fill in a form using Javascript/ajax/php but the problem is that my function only fills in one of the needed forms and stops even tho I have gotten the second response from the server.
我试图使用Javascript / ajax / php填写表单,但问题是我的函数只填写所需的表单之一,甚至停止我从服务器得到第二个响应。
Code:
码:
The function that starts filling stuff
开始填充东西的功能
function luePankkiviivakoodi(str) {
if (str==null) { //are we NOT injecting variables directly into the code, if not - Prompt for the barcode, and set the variable
var str = prompt("Valmis vastaanottamaan", "");
}
if (str==null) { //someone pressed abort on the prompt, we return
return;
}
newstr = str.split(' ').join(''); // remove spaces
if (str=="") { //is the string empty? -> return
return;
}
if (window.XMLHttpRequest) { //AJAX code
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
eval(xmlhttp.responseText);
//we set some fields, no problem
document.getElementById('P_VII').value = viite;
document.getElementById('IBAN').value = saajatili;
document.getElementById('laskun_summa').value = summa;
document.getElementById('eräpäivä').value = eräpäivä;
//trigger other functions
getKassasumma(summa); //AJAX for accesing the database and calculating the sale price
DevideIntoCells(); //AJAX for accessing the database and dividing a sum into different cells
validateSumma(); //Validates the sum, and tells the user if it's OK
}
}
xmlhttp.open("GET","dataminer.php?question=pankkiviivakoodi&q="+newstr,true);//open AJAX connecttion
xmlhttp.send();//send stuff by AJAX
}
}
getKassasumma:
getKassasumma:
function getKassasumma(str) {
if (str=="") {
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
eval(xmlhttp.responseText);
}
}
kale = document.getElementById("TOS_K_ale").value;
xmlhttp.open("GET","dataminer.php?question=kassasumma&q="+str+"&kale="+kale.replace("%", "p")+"&nro="+document.getElementById("S_NRO").value,true);
xmlhttp.send();
}
DevideIntoCells:
DevideIntoCells:
function DevideIntoCells() {
str = document.getElementById('tiliöintitapa').value;
if (str==null) {
return;
}
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
document.getElementById("spinwheel3").style.visibility = "visible";
}
xmlhttp.onreadystatechange=function() {
//alert('OK! val= '+xmlhttp.readyState);
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
//alert('OK!');
eval(xmlhttp.responseText);
//alert('OK2!');
document.getElementById("spinwheel3").style.visibility = "hidden";
//alert('OK3!');
calculateSumma();
}
}
xmlhttp.open("GET","dataminer.php?question=percentages&q="+str+"&nro="+document.getElementById('S_NRO').value,true);
xmlhttp.send();
}
validateSumma (just some math):
validateSumma(只是一些数学):
function validateSumma() {
float = document.getElementById('summabox').value;
float = float.replace(",",".");
summa = parseFloat(float);
if (summa < 0) {
summa = 0
};
kassasummaunp = document.getElementById('laskun_summa').value;
kassasummafloat = kassasummaunp.replace(",",".");
kassasumma = parseFloat(kassasummafloat);
if (kassasumma < 0) {
kassasumma = 0
};
if (kassasumma == 0 || summa == 0) {
prosentti = "0%";
}
else {
prosentti = summa / kassasumma * 100;
prosentti = Math.round(prosentti*Math.pow(10,2))/Math.pow(10,2);
prosentti = prosentti+"%";
};
if (prosentti == "100%") {
is100 = 1;
}else {
is100 = 0;
}
document.getElementById('prosentti').innerHTML = prosentti;
if (is100 == 1) {
document.getElementById('prosentti').setAttribute("style", "color:green");
} else {
document.getElementById('prosentti').setAttribute("style", "color:red");
}
puuttuvaEuro();
}
The problem code here is getKassasumma(summa); and DevideIntoCells();. I disable one of them, and the other one works, I enable both of them, DevideIntoCells stops somewhere before document.getElementById("spinwheel3").style.visibility = "hidden";, probably at the eval(response) because getKassasumma already finished the ajax request and killed this one. same the other way around.
这里的问题代码是getKassasumma(summa);和DevideIntoCells();.我禁用其中一个,另一个工作,我启用它们,DevideIntoCells在document.getElementById(“spinwheel3”)之前的某处停止.style.visibility =“hidden”;可能在eval(响应)因为getKassasumma已经完成了ajax请求并杀死了这个。反过来说。
AJAX answers: DevideIntoCells:
AJAX答案:DevideIntoCells:
var KP_osuus = parseFloat('40');
laskunsumma = parseFloat(document.getElementById('laskun_summa').value);
onepercent = laskunsumma/100;
newvalue = onepercent*KP_osuus;
document.getElementById('box1.5').value = newvalue;
var KP_osuus = parseFloat('60');
laskunsumma = parseFloat(document.getElementById('laskun_summa').value);
onepercent = laskunsumma/100;
newvalue = onepercent*KP_osuus;
document.getElementById('box2.5').value = newvalue;
AJAX answer: getKassasumma
AJAX回答:getKassasumma
var kassasumma = '477.99€';
document.getElementById('kassasumma').value = kassasumma;
Please ask if you need clarification!
请询问您是否需要澄清!
EDIT: Just to be clear, this is NOT an AJAX problem, rather javascript.
编辑:为了清楚,这不是一个AJAX问题,而是javascript。
2 个解决方案
#1
0
Try to make xmlhttp
local, i.e.
尝试使xmlhttp本地,即
var xmlhttp;
Because you are overwriting you xmlhttp
you refer to in the event listeners, so when the listeners get called, they both see the same response.
因为你覆盖了你在事件监听器中引用的xmlhttp,所以当监听器被调用时,它们都会看到相同的响应。
at the beginning of every of your functions. For compatibility, also use send(null)
instead of send()
.
在每个功能的开头。为了兼容性,还使用send(null)而不是send()。
#2
1
I think you are 'swimming in it', how we say. If you begin with AJAX, I'd recommend you use a framework like jQuery and it's $.get()
or $.post()
functions. It will accomplish all the needed AJAX logic for you.
我们怎么说,我认为你正在“游泳”。如果你从AJAX开始,我建议你使用像jQuery这样的框架,它是$ .get()或$ .post()函数。它将为您完成所有需要的AJAX逻辑。
#1
0
Try to make xmlhttp
local, i.e.
尝试使xmlhttp本地,即
var xmlhttp;
Because you are overwriting you xmlhttp
you refer to in the event listeners, so when the listeners get called, they both see the same response.
因为你覆盖了你在事件监听器中引用的xmlhttp,所以当监听器被调用时,它们都会看到相同的响应。
at the beginning of every of your functions. For compatibility, also use send(null)
instead of send()
.
在每个功能的开头。为了兼容性,还使用send(null)而不是send()。
#2
1
I think you are 'swimming in it', how we say. If you begin with AJAX, I'd recommend you use a framework like jQuery and it's $.get()
or $.post()
functions. It will accomplish all the needed AJAX logic for you.
我们怎么说,我认为你正在“游泳”。如果你从AJAX开始,我建议你使用像jQuery这样的框架,它是$ .get()或$ .post()函数。它将为您完成所有需要的AJAX逻辑。