我的.php正在读取pos.txt中的一些变量,我需要在不刷新页面的情况下直接显示它们。我使用过<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
,但这很烦人。我读过一些关于ajax的内容,但我真的不明白它是如何工作的。
$line = file_get_contents('pos.txt');
list($date, $time, $x, $y, $z) = explode(' ', $line);
答案 0 :(得分:2)
为此你必须使用AJAX。你必须学习这个http://www.w3schools.com/ajax/default.asp。 一旦你学会了它,你将永远使用它。
答案 1 :(得分:0)
只需从显示页面调用ajax调用到你的php文件。
var ajax = new XMLHttpRequest();
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && xmlhttp.status==200)
{
var response = ajax.responseText;
// Here is your response
}
}
ajax.open("POST", "request/path/response.php");
ajax.send(any_data);
答案 2 :(得分:0)
最简单的方法是使用jquery ajax:
http://api.jquery.com/jquery.ajax/
你想做这样的事情:
$.ajax({
url: "pos.txt",
}).done(function(data) {
var split = data.split(' ');
var date = split[0];
var time = split[1];
var x = split[2];
var y = split[3];
var z = split[4];
//then insert these variables into the elements you need
$('#date').val(date);
});
答案 3 :(得分:0)
http://api.jquery.com/jquery.ajax/
$.ajax({
url: "[_YOUR_URL_]/post.txt",
}).done(function(data) {
$("#some_id").val(date.find("some data").text);
});
上面的代码显然不会起作用,但您将使用的代码就是这么简单。 如上所述,一旦你去了ajax,你就不会回去了。
使用jQuery包装器可以更轻松地实现ajax包。您将需要花一两个小时阅读它以及查看各种样本
答案 4 :(得分:0)
试试这个会起作用:
<html>
<head>
<script>
var ajax = new XMLHttpRequest();
ajax.onreadystatechange=function()
{
if (ajax.readyState==4 && xmlhttp.status==200)
{
var response = ajax.responseText;
document.getElementById('get-data').innerHtml = response;
}
}
ajax.open("GET", "pos.txt");
ajax.send(any_data);
</script>
</head>
<body>
<div id="get-data">
</div>
</body>
</html>