【发布时间】:2014-01-14 16:33:19
【问题描述】:
我想制作一个有按钮的网站,当你点击它时,让它改变一些文本(说真/假)。当用户单击按钮时,文本必须更改,但不仅针对一个用户,还针对站点上的每个用户。
我尝试这样做的方式是我有一个文本文件,到目前为止,我所拥有的只是页面上的一些文本,每 500 毫秒刷新一次以显示文本文件中的任何内容。
所以现在我需要做的就是在单击按钮时更新文本文件。我能做到这一点的最好方法是什么? (我希望能够按下托管该站点的计算机或访问该站点的另一台计算机上的按钮。)
谢谢, Fjpackard。
更新
index.php:
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<body>
<div id="theDiv"></div>
</body>
<script>
$("document").ready(function(){
$("button").remove();
setInterval(function(){
$("#theDiv").load("file.txt");
},500);
var deviceAgent = navigator.userAgent.toLowerCase();
var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
if (agentID) {
// mobile code here
}
else {
$("body").prepend("<button>Toggle</button>");
$("#theDiv").css("visibility","none");
$("button").click(function(){
myClick();
});
}
});
function myClick() {
var url = ''; //put the url for the php
value = $.post("", {}).done(
function(data) {
$('#theDiv').html(data);
}
);
}
</script>
script.php:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST')
{
$file = 'file.txt';
$previous = file_get_contents($file);
if ($previous === 'true')
{
file_put_contents($file, 'false');
echo 'false'; //this is what we return to the client
}
else
{
file_put_contents($file, 'true');
echo 'true'; //this is what we return to the client
}
exit();
}
?>
【问题讨论】:
-
你有什么尝试吗?有代码示例吗?
-
我试过了:$fp = fopen('file.txt', 'w'); fwrite($fp, '假'); fclose($fp);并且:file_put_contents('file.txt','test');还没有运气:(
-
我在Use buttons to post to a php script 看到了类似的结构。我会尽量适应你的情况。
-
谢谢!怎么样了...? :)
标签: php jquery text-files overwrite