jaja
jaja

Reputation: 1

create notification when user insert new order into database in php

I recently developed a website for purchasing online. My problem is how I can create notification to alert at admin page when new order is inserted into MySQL. This notification will appear as an icon number at admin page on website, so that when admin open website they will know new order had made by customer.

I know we have to use javascript ajax but I have not idea how to begin.

I have some example code:

refreshInterval = 500
refreshTimeout = setTimeout(getNotificationCounts, refreshInterval);

function getNotifications() {
  $.ajax({
    url: 'path_to_your_php',
    type: 'POST',
    data: //put for example user_id but it cvan be handled by sesion as    
    dataType: 'json',
    success: function (data) {
      alert('You have' + data.total + ' new messages')
      refreshTimeout = setTimeout(getNotificationCounts, refreshInterval);
    }
  });
}

Upvotes: 0

Views: 3944

Answers (1)

Alkis Kalogeris
Alkis Kalogeris

Reputation: 17745

There are two alternatives for your problem. What you are looking for is persistent http connection.

  1. If you don't mind the browser support of your solution, you could use websockets. They are part of the html5 api but they are not supported by all the browsers and it's a new thing so there will be support on new versions only. Check here for more http://www.html5rocks.com/en/tutorials/websockets/basics/
  2. The second alternative is long polling. The basic idea is that the client makes an ajax call to the server periodically but doesn't return immediately, it returns if the server has a response, or if a specific amount of time has been reached. You could do simple polling too, but it's not a good practice because you make a lot of calls to the server with no reason most of the time. The long polling technique is also known as comet technique. Look into that, and come back with some more focused questions.

How do I implement basic "Long Polling"?

Here you can find a basic example of what you want to do. It's detailed too.

Upvotes: 1

Related Questions