Can we use ajax with interval to request info from the database?
You can use Javascript setInterval() function to run ajax request at regular intervals.
2 Likes
what is considered a regular interval. because so far im only getting one return and seems the rest are being blocked.
what are your codes? plus the file that you want get its data via ajax
<script type="text/javascript">
var preid;
function loop(){
var element = document.getElementById("start");
element.parentNode.removeChild(element);
window.setInterval(function getform()
{
var XHR = new XMLHttpRequest();
var form = document.getElementById("messageform");
var FD = new FormData(form);
console.log(XHR);
XHR.onload = () =>{
var response
console.log(XHR.responseText);
try{
response = JSON.parse(XHR.responseText);
}catch (e){
console.log(XHR.responseText);
console.error('no parse');
}
if(response){
handleresponse(response);
}
}
XHR.open("POST", "update.php");
XHR.send(FD);
}, 2000);
}
function handleresponse(response){
console.log(response.message);
var message = response.message;
var id = response.id;
var ok = response.ok;
if(ok){
addElement("infobox", "div", "message", message);
document.getElementById("id").value = id;
}else{
}
}
function addElement(parentId, elementTag, elementId, html) {
// Adds an element to the document
var p = document.getElementById(parentId);
var newElement = document.createElement(elementTag);
newElement.setAttribute('id', elementId);
newElement.innerHTML = html;
p.appendChild(newElement);
}
</script>
update.php
mysqli_close($db);
$response = "<div id='message'>".$username.": > ".$message."</div>";
$data = array("message"=>$response, "id" => $id, "ok" => $ok);
echo json_encode($data);
{“message”:“
aracnophob: > request</div>”,“id”:null,“ok”:false}
priceless.php:38
priceless.php:17 XMLHttpRequest {onreadystatechange: null, readyState: 0, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
priceless.php:20
priceless.php:38
aracnophob: > request
priceless.php:17 XMLHttpRequest {onreadystatechange: null, readyState: 0, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
priceless.php:20
{“message”:“
aracnophob: > send</div>”,“id”:null,“ok”:false}
priceless.php:38
priceless.php:38
aracnophob: > send
request comes through in first itteration. send did not
ahh. it keeps sending my id back as null. on my local server here it works just fine. but on the epizy host it doesnt.
yeah looks like its being blocked by the server
What is being blocked by the server how? A PHP script which gets data from somewhere returns null where it should return a value?
1 Like
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.