我迷失了寻找解决方案的道路,需要立即帮助。我有一个名为list.php的php页面,它显示每个产品上带有购买图标的产品列表。单击“购买”按钮后,它将指向action.php (其中包含要添加到数据库中的脚本),然后使用标头(location:list.php?id=$rid_product)返回到同一页。在返回到list.php后,我想显示一条弹出消息“产品现在被添加到购物车”。怎么做?
发布于 2014-10-28 08:28:22
您可以使用cookie,返回后只需检查是否存在指定的cookie,如果存在,则显示消息。
但是,如果使用ajax,则不需要cookie,只需在ajax解析函数中添加弹出函数,类似于此。
function addToBasket(itemId) {
var x = new XMLHttpRequest();
x.open('GET', '/ajax/action.php?add=' + itemId, true);
x.onreadystatechange = function() {
if (x.readyState == 4) && (x.status == "200") {
var responseObj = eval('(' + x.responseText + ')');
if (responseObj.status == 0)
window.alert('The product successfully added to your basket');
else
window.alert('An error has occured during the processing the data. Please try again later');
}
};
x.send(null); action.php代码示例
<?php
// ...
$response = array('status' => 1);
if (isset($_GET['itemId'] && $_GET['itemId'] != '')
{
$item_id = htmlspecialchars($_GET['itemId']);
if (is_numeric($item_id)
{
put_in_table($item_id);
$response = array('status' => 0);
}
}
// output an Ajax response
header('Content-Type: application/javascript');
echo json_encode($response);
?>发布于 2014-10-28 08:30:41
在action.php上创建会话或Cookie
如果发现显示弹出,请检查list.php
在弹出后销毁它,这样它就会一次又一次地显示出来。
发布于 2014-10-28 08:33:41
您在标题中提到了ajax,那么为什么不使用ajax对action.php进行发布,并且在ajax请求成功完成后,您可以显示弹出窗口。
https://stackoverflow.com/questions/26603704
复制相似问题