【发布时间】:2012-01-29 04:44:07
【问题描述】:
我想在不刷新页面的情况下从数据库中提取一些数据。最好的方法是什么?
我正在使用以下 XMLHTTPRequest 函数从 cart.php 文件中获取一些数据(购物车项目)。该文件根据option 值执行各种功能。
例如:option=1 表示获取所有购物车商品。 option=2 表示删除所有购物车项目并返回字符串"Your shopping cart is empty."。 option=3, 4...等等。
我的 XHR 函数:
function getAllCartItems()
{
if(window.XMLHttpRequest)
{
allCartItems = new XMLHttpRequest();
}
else
{
allCartItems=new ActiveXObject("Microsoft.XMLHTTP");
}
allCartItems.onreadystatechange=function()
{
if (allCartItems.readyState==4 && allCartItems.status==200)
{
document.getElementById("cartmain").innerHTML=allCartItems.responseText;
}
else if(allCartItems.readyState < 4)
{
//do nothing
}
}
var linktoexecute = "cart.php?option=1";
allCartItems.open("GET",linktoexecute,true);
allCartItems.send();
}
cart.php 文件看起来像:
$link = mysql_connect('localhost', 'user', '123456');
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db('projectdatabase');
if($option == 1) //get all cart items
{
$sql = "select itemid from cart where cartid=".$_COOKIE['cart'].";";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
echo $row['itemid'];
}
}
else if($option == 2)
{
//do something
}
else if($option == 3)
{
//do something
}
else if($option == 4)
{
//do something
}
我的问题:
- 有没有其他方法可以从数据库中获取数据而无需 刷新页面?
- 是否存在任何潜在威胁(黑客攻击、服务器利用、 性能等)以我做这件事的方式?我相信黑客 可以使用 option=1 淹没我的服务器发送不必要的请求, 2、3等
【问题讨论】:
标签: javascript php mysql xmlhttprequest