hkguile
hkguile

Reputation: 4369

How to use php to recevie notification (remote post) and run javascript?

I have web application e.g A, A will generate and pop up B page (a QR code), after the users scanned a QR code, the thrid party server will send a "$_POST" to A application to http://example.com/notify/ , how do i create a php function to echo some javascript (A page will keep opening)?

I've tried added to A application's controller but it is not work

echo "success";     
echo '<script language="javascript">';
echo 'alert("message was received")';
echo '</script>';

Upvotes: 0

Views: 46

Answers (1)

Quentin
Quentin

Reputation: 944171

There are two basic approaches you can take.

  1. Polling
  2. Server push

Polling is simpler but generally less efficient.

When the HTTP request from the third-party server is received by your server, record the information in it (e.g. in a database).

The page you want to show the result on should use JavaScript (e.g. the XMLHttpRequest object) to make a request to a URL on an interval.

That URL should return the information you stored in the database (or a notification that said data isn't available yet).

Server push is more complicated, but more efficient. The browser keeps a connection open to the server and data can be sent along the connection in either direction at any time. This is usually implemented with Web Sockets.


Either way, you should send data along the connection (i.e. message was received) and not JavaScript. Have the JavaScript that presents the message to the user be part of the code which waits for the message and not part of the message itself.

Upvotes: 1

Related Questions