0

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>';
hkguile
  • 4,235
  • 17
  • 68
  • 139
  • 1
    If a`POST` request is sent by a third party to your your application, it doesn't make sense to return javascript back to the third party making the `POST`. Can you update the question to clarify. – Lex May 28 '18 at 11:02

1 Answers1

1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335