How to scale PHP voting system for multiple posts?
I have a working PHP Ajax voting system that writes the likes from a blog post into a .txt file, and I want to scale it for multiple posts and record the likes from each of those.
I have tried to change the "onclick" value, but it seems that the script I am using limits me. I have done extensive research on Stackoverflow and other platforms, and tried many implementations. Please, can you assist with my below code, and point me to the right direction?
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);
?>
There are 2 Problems here:
1. you cannot name your vaiable int
this is a reserved keyword so rename it to x
2. you're js-fiddle is set to onload. this will wrap your javascript in a function. In your fiddle go to the Javascript + no library (pure js)
selector then select No-wrap bottom of head
in the Load Type
options
Reserved Javascript-keywords
Now: about scaling.
If you want to scale this and not use a database... you can either have a voting file for every post. then ud just pass the post name to the ajax call and your php script would open the file, read the number, increment it by 1 and close it. OR you could store them in a Single file and parse the content when u need it. so i.E ud have a file that consists of <postid>:<votes>,..... and parse this file using explode. However the question of how to scale this is in general Too broad