Reputation: 567
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.
Upvotes: 3
Views: 143
Reputation: 2377
I think you dont have a proper database and you only want to do it using a text file. use Ajax to write in the text file about the last number of vote count done. Code will look something like this.
CODE
$.ajax(function(){
url:"voteup.php" //here you wrtie some function in php which takes care of file I/o
data:{votecount:9}//last vote count
success:function(){alert("success");}
}); // this function should to write new votes in your text file using ajax.
Now to read current votes on body onload. You have call a different ajax method to read that text file and get the current vote count..
CODE
function opstart()
{
$.ajax(function(){
url:"getvotes.php" //here you wrtie some function in php which takes care of file I/o
success:function(){alert("success");}
}); // this function should to read current votes in your text file using ajax.
}
Upvotes: 0
Reputation: 5399
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
Upvotes: 1
Reputation: 121
You could use a form as follows:
var feature_form =new Ext.form.FormPanel({
id: "featureInfo_panel",
url: 'myfile.php',
autoDestroy:true,
frame: true,
width: 410,
Where 'myfile.php' points to the name an location of the php you want to pass / get data from. The php can easily trawl text files from there......
Upvotes: 0
Reputation:
you can study following tutorial
Upvotes: 0