Php 从数据库自动更新表而不刷新页面
我不熟悉php和ajax 我面临的问题是- 我正在jsp页面中显示一个表。。我想自动更新表格,而不需要 每10秒刷新一次页面 我正在从php页面检索数据库的值 下面是data.jsp的代码Php 从数据库自动更新表而不刷新页面,php,html,ajax,Php,Html,Ajax,我不熟悉php和ajax 我面临的问题是- 我正在jsp页面中显示一个表。。我想自动更新表格,而不需要 每10秒刷新一次页面 我正在从php页面检索数据库的值 下面是data.jsp的代码 <html> <script type="text/javascript"> function Ajax() { var $http, $self = arguments.callee;
<html>
<script type="text/javascript">
function Ajax()
{
var
$http,
$self = arguments.callee;
if (window.XMLHttpRequest) {
$http = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
$http = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
$http = new ActiveXObject('Microsoft.XMLHTTP');
}
}
if ($http) {
$http.onreadystatechange = function()
{
if (/4|^complete$/.test($http.readyState)) {
document.getElementById('ReloadThis').innerHTML = $http.responseText;
setTimeout(function(){$self();}, 10000);
}
};
$http.open('GET', 'getuser.jsp', true);
$http.send(null);
}
}
function myFunction()
{
setTimeout(function() {ajax();}, 10000);
}
</script>
</head>
<body>
<button onclick="myFunction()">Try it</button>
<div id="ReloadThis" > the table is to be shown here </div>
</body>
</html>
函数Ajax()
{
变量
$http,
$self=arguments.callee;
if(window.XMLHttpRequest){
$http=newXMLHttpRequest();
}else if(window.ActiveXObject){
试一试{
$http=newActiveXObject('Msxml2.XMLHTTP');
}捕获(e){
$http=newActiveXObject('Microsoft.XMLHTTP');
}
}
如果($http){
$http.onreadystatechange=function()
{
如果(/4 | ^complete$/.test($http.readyState)){
document.getElementById('ReloadThis')。innerHTML=$http.responseText;
setTimeout(函数(){$self();},10000);
}
};
$http.open('GET','getuser.jsp',true);
$http.send(空);
}
}
函数myFunction()
{
setTimeout(函数(){ajax();},10000);
}
试试看
此表将在此处显示
我将在php中执行getuser部分。但是由于tomcat不支持php。。因此,我将代码转换为jsp。。这是我的密码
和getuser.jsp的代码
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%
String cond="SELECT * FROM invertor ";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:xe");
PreparedStatement ps = con.prepareStatement(cond);
ResultSet rs = ps.executeQuery();
if (rs.next())
{
dc_volt = rs.getString(1).trim();
dc_amp = rs.getString(2).trim();
ac_volt = rs.getString(3).trim();
ac_amp = rs.getString(4).trim();
}
else
{
}
%>
我得到的错误是java.lang.arrayindexoutofboundsexception:2
在从my data.jsp页面触发“try”:按钮后。。没有数据库表显示…您的代码应该是这样的-
<script>
if ($http) {
/*make the call*/
$http.open('GET', 'getuser.php', true);
/* check for state change */
$http.onreadystatechange = function()
{
if (/4|^complete$/.test($http.readyState)) {
document.getElementById('ReloadThis').innerHTML = $http.responseText;
setTimeout(function(){$self();}, 10000);
}
};
/*Because this is a GET request, the send values can be null*/
$http.send(null);
}
</script>
如果($http){
/*打电话*/
$http.open('GET','getuser.php',true);
/*检查状态更改*/
$http.onreadystatechange=function()
{
如果(/4 | ^complete$/.test($http.readyState)){
document.getElementById('ReloadThis')。innerHTML=$http.responseText;
setTimeout(函数(){$self();},10000);
}
};
/*因为这是一个GET请求,所以发送值可以为null*/
$http.send(空);
}
您一直面临的问题是什么?您正在声明$q=$\u GET[“q”]
但不使用它,您想在数据库中搜索并返回值时使用它吗?我编辑了我的php。。检查一下。。问题是我没有得到任何更新的结果。。