3

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.

Pavenhimself
  • 527
  • 1
  • 5
  • 18

4 Answers4

1

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

Community
  • 1
  • 1
Zevi Sternlicht
  • 5,399
  • 19
  • 31
  • If this is a small side project for OP then, sometimes, having a large database is cumbersome. For a simple solution with a website that doesn't get many hits a flat file is a good solution too! (way less CPU load and lines of code) – dcbarans Aug 29 '12 at 15:01
0
  1. Create one php page which accept your count and store in db
  2. make ajax call in "opstart" function.

you can study following tutorial

http://net.tutsplus.com/tutorials/html-css-techniques/building-a-5-star-rating-system-with-jquery-ajax-and-php/

Ketan Parmar
  • 2,962
  • 19
  • 27
  • 1
    There is no need for ajax in this case if the page is being refreshed. – yehuda Aug 29 '12 at 12:18
  • as per question,page does not refresh on clicking submit vote button – Tarun Aug 29 '12 at 12:21
  • @fluty what are you talking about, i quote, `Then when he uses 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?` – yehuda Aug 29 '12 at 12:24
0

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......

0

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.

}
Ashirvad
  • 2,367
  • 1
  • 16
  • 20
  • I have not chosen how to save the data, because this field with html, php and javascript is very unknown to me. But the database sounds like a good idea. I'll try look into that, thank you. – Pavenhimself Aug 29 '12 at 12:31