PHP run from web page A to load content from database in web page B
I am creating a chat and everything is working fine.
When person A creates a message in the text box and clicks the SEND button of the HTML form, PHP submits it to the server.
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect textarea content
// inserted (appended) to mySQL table.
// collect chat content in mySQL table
// display chat content in a <div>
}
?>
(I am not posting the PHP code because it is irrelevant to the problem)
Person B receives the message due to javascript setInterval, which triggers a function that grabs the chat content from the database via AJAX and then displays the chat content for person B in a div.
setInterval(refreshChat, 1000);
function refreshChat() {
$.ajax({
url: "http://eyesonpi.com/_/php/chat.php",
type: "post",
success: function(data) {
$("#chatWindow").html(data);
},
error: function() {
$("#chatWindow").prepend("Error");
}
});
}
When I tried this on my iPhone, running IOS 10, the AJAX call failed. IOS 10 has a problem with AJAX. https://forums.developer.apple.com/thread/64526
I've tried with simple Javascript (xmlHttpRequest) and with jQuery.
I tried my hosting company chat box from my iPhone and their chat works. I don't know where to start with the workaround. How can a person receive a message from person A without reloading the page? Any suggestions?
Thank.
source to share
You can try to use websockets for chat. Or a long association technology that allows communication between the server and the client (browser or mobile application). if you choose websockets. Then you can try using server side: Workerman or Ratchet . their work. All modern browsers support websocket, or you can use popular libraries like sockJS for cross browser support.
source to share
Try to replace the behavior setInterval
with setTimeout
, otherwise the previous request will be killed if the next one is sent
setTimeout(refreshChat, 0);
function refreshChat() {
$.ajax({
url: "http://eyesonpi.com/_/php/chat.php",
type: "post",
success: function (data) {
$("#chatWindow").html(data);
},
error: function () {
$("#chatWindow").prepend("Error");
}
}).always(function () {
setTimeout(refreshChat, 1000);
});
}
source to share