简体   繁体   中英

PHP-Jquery-AJAX, show popup message

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?

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);
?>

Create a session or Cookie on action.php

Check on list.php if find show popup

Destroy it on after showing popup so it will now show again and again.

您在标题中提到了ajax,所以为什么不使用ajax将其发布到action.php,成功完成ajax请求后,您可以显示弹出窗口。

<?php if( isset($_POST['id']) && $_POST['id']>0 ){ ?> alert(message); <?php } ?>

about the popup thingy, check that link Launch Bootstrap Modal on page load , it's called bootstrap modal, very simple to use. You have a small script to write in order to launch it on page load.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM