I am trying to make it possible for users to vote on some of my pictures online.
I am writing all my code in HTML, JavaScript and PHP.
When the user presses the vote button, it counts 1 up. Then when the user refreshes the page, I want to keep the vote, so it will still say 1, instead of resetting to zero.
My question is, how can I do this?
I found out I can’t use javascript fileIO on my server.
I tried with some PHP, but most my code is in javascript and I can’t figure out how to execute some code from a javascript function.
I have something like this in mind:
<body onload="opstart();">
When the body is loaded, I call a javascript function. Can I call some PHP here?
// Get number of votes from txt file
function opstart()
{
}
Inside this, I was thinking about reading the data from a text file and load it into the variable holding the number of votes.
Why are you storing these values in a text file. They should be in a database where you can easily pull them out in PHP. This will save you tons of time is much better practice.
You will need a users table with an ID for each user, an image table with an ID for each image, and a votes table recording who voted on what image ID. You then simply count the votes for each thing voted, and to stop someone from voting twice you can check if he has already voted!
See this answer for more details