dtsps00544 2014-10-28 08:25
浏览 55
已采纳

PHP-Jquery-AJAX,显示弹出消息

I lost my path to finding the solution and need help immediately. I have a php page called list.php which shows list of products with BUY icon on each product. Once the BUY button clicked, it will directing to action.php (which contain script to add into the database) and then return into the same page using header(location:list.php?id=$r[id_product]). I want to show a popup message "the product is now added to cart" after returned into the list.php. How to do that?

  • 写回答

5条回答 默认 最新

  • douluo7366 2014-10-28 08:28
    关注

    You can you cookie and after returning just check if there is specified cookie or not and if there is, show message.

    However if you using ajax you don't need a cookie, just add pop-up function in ajax parsing function, something like that

    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 code example

    <?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);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)