Yupiter Aryo
Yupiter Aryo

Reputation: 11

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?

Upvotes: 0

Views: 1453

Answers (5)

Mark Zucchini
Mark Zucchini

Reputation: 925

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

Upvotes: 1

oligan
oligan

Reputation: 634

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.

Upvotes: 0

Ra&#250;l Monge
Ra&#250;l Monge

Reputation: 223

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

Upvotes: 0

Michael Westcott
Michael Westcott

Reputation: 480

You mention ajax in the title, so why don't you use ajax to do the post to action.php, and on successful completion of the ajax request you can display the popup.

Upvotes: 0

bhupendra
bhupendra

Reputation: 43

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.

Upvotes: 0

Related Questions