使用AJAX和PHP时缓存(可能)的问题

时间:2009-11-25 17:15:46

标签: php ajax winforms forms caching

首先,我尝试了一些类似的帖子但却找不到任何东西。主要是因为我在FF中看到了这个问题,而不仅仅是IE。

我有一个更新MySQL数据库的网页没有问题。然后今天,我改变它的方式,这样我可以使用ID而不是值,所以对于大小,而不是小,中,大,它是1,2和3。

相关网站为here,您可以输入代码1234进入测试图库。

每次添加到购物篮时,我都会将脚本设置为outoput。

因此,在单选按钮下,有值1,2和3。

此位的HTML如下:

<input type="radio" name="imageSize" id="imageSize" value="1"/> Small (6x4) - &pound;4.99<br/>
<input type="radio" name="imageSize" id="imageSize" value="2"/> Medium (7x5) - &pound;6.99<br/>
<input type="radio" name="imageSize" id="imageSize" value="3"/> Large (8x6) - &pound;10.99

我现在决定手动编写代码以使其正常工作。

将其发送到包含的JavaScript文件,然后发送到插入页面,如下所示:

unction createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/* -------------------------- */
/* INSERT */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Just a second..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var image= encodeURI(document.getElementById('image').value);
var image_Size= encodeURI(document.getElementById('image_Size').value);

alert(image_Size);
alert(nocache);

// Set the random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('get', 'insert.php?image='+image+'&imageSize='+image_Size+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
}
function insertReply() {
if(http.readyState == 4){
var response = http.responseText;
// else if login is ok show a message: "Site added+ site URL".
document.getElementById('insert_response').innerHTML = 'Image added:'+response;
}
}

正如您所看到的,无论发生什么,警报弹出窗口都会显示1.即使您选择中型或大型。这是怎么回事?

1 个答案:

答案 0 :(得分:1)

这里的问题是你正在打电话

document.getElementById('image_Size').value

像Lizard所说,你有3个具有相同ID的输入。 document.getElementById将返回带有ID的第一个元素,因为ID应该是唯一

这是一种获取所选值的方法,因为你已经包含了jQuery,我将为你在jQuery中编写它。

var image_Size = 0;
$('[name=image_Size]').each(function() {
   if ($(this).attr('checked')) image_Size = $(this).val();
});

一点点谷歌搜索发现这节省时间!

var image_Size = $('[name=image_Size]:checked').val();

我希望这有帮助!而且,您可能希望以不同方式设置nocache:

nocache = new Date().getTime();

这将为您提供自Unix时代以来ms的时间,因此每次用户请求页面时都会有所不同。

如果您需要有关jQuery API的更多帮助,建议您启动here

我希望这有帮助!

-Stephen