0

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 Answers5

1

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);
?>
Mark Zucchini
  • 925
  • 6
  • 11
  • I'm using script from http://jsfiddle.net/pX5kr/ but still not working. Is it same or different thing? – Yupiter Aryo Oct 28 '14 at 08:43
  • In my browser it is woking. Almost same, just in your example above you achieved the result trhough `jquery`. In my example I used pure javascript. They both are gives same results. Choose what you prefferd. – Mark Zucchini Oct 28 '14 at 08:48
0

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.

bhupendra
  • 43
  • 3
  • 10
0

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.

0

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

Raúl Monge
  • 223
  • 2
  • 7
0

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.

Community
  • 1
  • 1
oligan
  • 634
  • 6
  • 18