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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!