如何在javascript中执行查询

如何在javascript中执行查询,javascript,Javascript,要在javascript上调用PhP请求,需要像这样使用xhr xhr.open('POST','myphp.php',true); setRequestHeader('Content-Type','application/json'); xhr.onreadystatechange=()=>{ 如果(xhr.readyState==4&&xhr.status==200){ mycallback(); } }; send(JSON.stringify({ myinfo:info, })); 您不

要在javascript上调用PhP请求,需要像这样使用xhr

xhr.open('POST','myphp.php',true);
setRequestHeader('Content-Type','application/json');
xhr.onreadystatechange=()=>{
如果(xhr.readyState==4&&xhr.status==200){
mycallback();
}
};
send(JSON.stringify({
myinfo:info,
}));

您不会直接在JavaScript代码中执行PHP代码。听起来您正在寻找的技术是“AJAX”,其中JavaScript代码向服务器发出HTTP请求,服务器可以是运行您喜欢的任何代码的PHP页面。有许多教程和示例可以帮助您开始使用PHP中的AJAX,可以使用您最喜欢的搜索引擎找到这些教程和示例。
const status = window.navigator.onLine;
if (status) online()
else offline()

window.addEventListener('online', online);
window.addEventListener('offline', offline);

function online() {
  document.getElementById('container').style.backgroundColor = 'green';
  //here i want execute a query like this-------> ($con,"SELECT * FROM users where online='yes' ");
}

function offline() {
  document.getElementById('container').style.backgroundColor = 'red';
  //here i want execute a query like this-------> ($con,"SELECT * FROM users where online='no' ");
}