[英]why is my ajax loading twice the same function?
I'm trying to create a subscription form which basically let's you select a game, then load's a bunch of subscription prices for that game. 我正在尝试创建一个订阅表单,基本上让您选择一个游戏,然后为该游戏加载一堆订阅价格。 Up to this point, everything works perfectly. 到此为止,一切都运行良好。 The problem is, when I try to input 3 radio buttons to select payement for 1 month/ 6 months/ 1 year, the function that I call with ajax onchange="" in the dropdown seems to load twice, and the content is not accessed. 问题是,当我尝试输入3个单选按钮来选择1个月/ 6个月/ 1年的付款时,在下拉列表中我用ajax onchange =“”调用的函数似乎加载了两次,并且无法访问内容。
The first time, it works perfectly, it echo's the information, and calculates everything. 第一次,它运行完美,它回显了信息并计算了一切。 but the second time, it just gives me a php error. 但是第二次,这只是给我一个PHP错误。
I've figured out what happens, it call's the function once, with the requested subscription price id (alert(planId);), and it echo's perfectly as I wanted it. 我已经弄清楚了会发生什么,它调用了一次函数,并带有请求的订阅价格id(alert(planId);),并且它完美地呼应了我的期望。 Then it just calls the function again, but the alert(planId); 然后,它只是再次调用该函数,但是alert(planId); returns me undefined, and echo's a new row (which was meant to happen) with the error code, saying that the sql request failed because of line 78. 返回我未定义的状态,并且echo带有错误代码的新行(本来是要发生的),并指出sql请求由于第78行而失败。
Which brings me back to my question, why does AJAX call the function twice? 这使我回到我的问题,为什么AJAX会两次调用该函数?
var xhr = {
planData:function(){
if (httpRequest.readyState==4) {
if (httpRequest.status==200) {
document.getElementById('plan_select').innerHTML = 'ready';
var price = httpRequest.responseText;
//alert(price);
var six = 6*parseInt(price);
var year = 12*parseInt(price);
if (document.getElementById('diff')) {
var ctnt = '';
ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="one" /> ';
ctnt += 'Month to month subscription plan ($'+price+' USD per month) ';
ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="six" /> ';
ctnt += 'Six months subscription plan ($'+six+' USD per 6 months) ';
ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="year" /> ';
ctnt += 'One year subscription plan ($'+year+' USD per year)';
document.getElementById('diff').innerHTML = ctnt;
} else {
var yearPrices = document.getElementById('order').insertRow(3);
var cell = yearPrices.insertCell(0);
cell.id='diff';
cell.colSpan = 3;
var ctnt = '';
ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="one" /> ';
ctnt += 'Month to month subscription plan ($'+price+' USD per month) ';
ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="six" /> ';
ctnt += 'Six months subscription plan ($'+six+' USD per 6 months) ';
ctnt += '<input class="radio" type="radio" name="typeOfPlan" value="year" /> ';
ctnt += 'One year subscription plan ($'+year+' USD per year)';
cell.innerHTML = ctnt;
}
}
}
},
updatePrice:function(planId){
alert(planId);
xhr.ajaxCall('GET', 'requests.php?action=getprices&planId='+planId, null, true, xhr.planData);
},
ajaxCall:function(method,url,postData,async,handler) {
httpRequest = new XMLHttpRequest();
httpRequest.open(method,url,async);
if (postData != null)
httpRequest.setRequestHeader("Content-Type","x-www-form-urlencoded");
if (async)
httpRequest.onreadystatechange = handler;
httpRequest.send(postData);
}
};
This is the part of the code, you'll see the alert(planId); 这是代码的一部分,您将看到alert(planId); in the updatePrice function. 在updatePrice函数中。
Here's a link to the actual working thing: http://www.nitroflox.com/temp/ 这是实际工作的链接: http : //www.nitroflox.com/temp/
I just looked at your supplied link. 我只是看了您提供的链接。 You are calling the updatePrice function twice: 您两次调用updatePrice函数:
onchange="xhr.updatePrice(xhr.updatePrice(this.options[this.selectedIndex].value))
Change to: 改成:
onchange="xhr.updatePrice(this.options[this.selectedIndex].value)
Let me know if it works. 让我知道它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.