ajax - PHP Poll with Results on Same Page -
there great article posted css-tricks.com describing how create poll using php , mysql database. i've followed , created nice poll myself. noticed in comments mentioning of using ajax show results on same page instead of separate page.
i wondering best way display php poll results on same page?
update:
the answer simple. in fact, css-tricks' poll without ajax in opinion more difficult since requires database. 1 not!
the complete tutorial creating poll php , ajax can viewed here:
http://www.w3schools.com/php/php_ajax_poll.asp
i wanted clarify how set arrays more 2 poll options. first "database" (i.e. text file, not mysql).
//get content of textfile $filename = "poll_result.txt"; $content = file($filename);
then put data in 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 text values also. if text value, nothing changes part of 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 results. file structure , ajax script, see complete tutorial.
you have 2 options:
1) use ajax suggested in question, when submit vote via ajax, results response.
2) results before vote submitted, still need submitted via ajax if want stay on same page. instead of using ajax results, since have results prior vote add 1 vote appropriate selection choice, use javascript / css change results hidden displayed.
Comments
Post a Comment