0

There was a great article posted by CSS-Tricks.com describing how to create a poll using PHP and a MySQL database. I've followed this and created a nice poll for myself. I noticed in the comments a mentioning of using AJAX to show the results on the same page instead of a completely separate page.

I am wondering what is the best way to display PHP Poll results on the same page?

UPDATE:

The answer is really simple. In fact, CSS-Tricks' poll without AJAX in my opinion is more difficult since it requires a database. This one does not!

The complete tutorial for creating a poll with PHP and AJAX is can be viewed here:

http://www.w3schools.com/php/php_ajax_poll.asp

I just wanted to clarify how to set the arrays up for more than two poll options. First you get the "database" (i.e. text file, not MySql).

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

Then you put the data in an array:

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

//if multiple options
$array = explode("||", $content[0]);
$option1array = $array[0];  //note: these values can be text values also.  If text value, nothing changes with this part of the code.  
$option2array = $array[1];
$option3array = $array[2];
$option4array = $array[3];

Store Data in "database"

if ($vote == 'option1')
  {
  $option1array = $option1array + 1;
  }
if ($vote == 'option2')
  {
  $option2array = $option2array + 1;
  }
if ($vote == 'option3')
  {
  $option3array = $option3array + 1;
  }
if ($vote == 'option4')
  {
  $option4array = $option4array + 1;
  }

Then, output your results. For file structure and AJAX script, see the complete tutorial.

Raphael Rafatpanah
  • 19,082
  • 25
  • 92
  • 158

1 Answers1

2

You have two options:

1) Use ajax just as you suggested in your question, when you submit the vote via ajax, get the results back as the response.

2) Get the results before the vote is submitted, it will still need to be submitted via ajax if you want to stay on the same page. But instead of using ajax to get back the results, since you have the results prior to the vote you could just add one vote to the appropriate selection choice, then use Javascript / CSS to change the results from hidden to displayed.

Pitchinnate
  • 7,517
  • 1
  • 20
  • 37
  • Hmm, it seems that option one is easier but I am getting the feeling that you prefer option two. Why? – Raphael Rafatpanah Aug 28 '13 at 13:31
  • I would use option one, but since in your question you said someone already recommended AJAX it seemed as though you were searching for another solution. Option 2 has holes in it, like if another person voted between the time of you going to the page and submitting you vote you wouldn't see that vote in the results. – Pitchinnate Aug 28 '13 at 13:35
  • It wasn't that I am searching for options beyond AJAX, it was just that I don't really know how to implement the poll with AJAX to achieve same page results. – Raphael Rafatpanah Aug 28 '13 at 13:43
  • Or, if AJAX was the best way. It seems that you approve AJAX as a solution. – Raphael Rafatpanah Aug 28 '13 at 13:44
  • You HAVE to use AJAX to submit the vote and stay on the same page, so it is only logical that you use the same AJAX call to retrieve the results. – Pitchinnate Aug 28 '13 at 13:45
  • Would you be able to point me in the right direction to accomplish something like this? For example, if the poll is located on `index.php`, then `
    ` ?
    – Raphael Rafatpanah Aug 28 '13 at 13:54
  • Actually I am going to do some more research on this and get back with either an updated full solution or some questions. – Raphael Rafatpanah Aug 28 '13 at 13:55
  • I'm sure if you do some searching you can find 10s of thousands of examples online. If you have problems with code you have written and tried then come back and ask a question. – Pitchinnate Aug 28 '13 at 13:55
  • thank you for your help. AJAX is really simple! I don't know why I was afraid of it. – Raphael Rafatpanah Aug 28 '13 at 17:21