PHP 多个帖子的投票系统

PHP Voting System for multiple posts

我有一个有效的 PHP Ajax 投票系统,可以将来自博客 post 的点赞内容写入 .txt 文件,我想将其扩展为多个 posts 并记录每个人的喜欢。

我曾尝试更改 "onclick" 值,但似乎我使用的脚本限制了我。

HTML

<span id="like"><a href="javascript:" name="vote"
value="0" onclick="getVote(this.value)">Like</a></span>

JAVASCRIPT

function getVote(int){
    if(window.XMLHttpRequest){
        xmlhttp=new XMLHttpRequest()
    }else{
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    }
   xmlhttp.onreadystatechange=function({
        if(this.readyState==4&&this.status==200{
            document.getElementById("like").innerHTML=this.responseText
        }
   };
    xmlhttp.open("GET","vote.php?vote="+int,true);
    xmlhttp.send()
}

PHP

<?php 
$vote=$_REQUEST['vote'];
$filename="votes.txt";
$content=file($filename);
$array=explode("-",$content[0]);
$yes=$array[0];
if($vote==0){
    $yes=$yes+1;
}
$insertvote=$yes;
$fp=fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>

Fiddle

我是否应该为每个 post 我想保存其点赞的文件编写一个不同的 .php 文件?

这里有两个问题:
1. 你不能命名你的变量 int 这是一个保留关键字所以重命名为 x
2.你的js-fiddle设置为onload。这会将你的 javascript 包装在一个函数中。在您的 fiddle 中转到 Javascript + no library (pure js) select 或者 select No-wrap bottom of head 中的 Load Type 选项 Reserved Javascript-keywords

现在:关于缩放。
如果你想扩展这个而不使用数据库......你可以为每个 post 有一个投票文件。然后 ud 只需将 post 名称传递给 ajax 调用,您的 php 脚本将打开文件,读取数字,将其递增 1 并关闭它。或者您可以将它们存储在一个文件中并在需要时解析内容。所以我有一个由 :,..... 组成的文件,并使用 explode 解析这个文件。然而,如何扩展这个问题通常 太宽泛