KahneFan Posted October 17, 2007 Share Posted October 17, 2007 Is it possible to use This AJAX flat poll, but allow multiple selections? I don't know much about SQL yet, so a flat poll is very helpful. I was able to give this poll multiple options with checkboxes, I just don't know what to do from there as far as writing to the text file. Example: What do you like better on a pizza __Pepperoni __Sausage __Tomato __Olives {VOTE} Guests could select Pepperoni as well as tomato and a +1 would be added to the txt file and then the results displayed on the results page. (Results) Pepperoni [ 1 ] Sausage [ 0 ] Tomato [ 1 ] Olives [ 0 ] Ultimately I would like to be able to say... What do you like better on a pizza __Pepperoni __Sausage __Tomato __Olives {VOTE} And with only one vote button it could calculate Pepperoni vs Sausage and Tomato vs Olives... Pepperoni [100%] Sausage [0%] Tomato [100%] Olives [0%] but I'm guessing this would take a SQL db and probably JS(?). Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 17, 2007 Share Posted October 17, 2007 Uhh, who said it needs a SQL database? It can use flat files for storing the information. For example, checkbox values are stored in arrays so you simply loop through for that. I'm assuming you know how to use the while() loop for arrays and file-writing/appending/updating. The AJAX would remain the same though, click the submit button, run the AJAX function to obtain the data, and you're done. Quote Link to comment Share on other sites More sharing options...
KahneFan Posted October 18, 2007 Author Share Posted October 18, 2007 The code above is not what I was using. I thought it was, but I have found what I am using... I changed the "radio" to "checkbox", but it will only register 1 at a time... display.php <form action="vote.php" method="post"> <table> <tr> <td> <input type="radio" value="0"> </td> <td> Bad </td> </tr> <tr> <td> <input type="radio" value="1"> </td> <td> Normal </td> </tr> <tr> <td> <input type="radio" value="2"> </td> <td> Good </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Vote"> </td> </tr> </table> </form> txt/vote.txt 0||0||0 vote.php $vote = $HTTP_POST_VARS['vote']; //the filename $filename = "txt/vote.txt"; //get content of textfile $content = file($filename); $array = explode("||", $content[0]); $bad = $array[0]; $normal = $array[1]; $good = $array[2]; if($vote == 0) $bad = $bad + 1; if($vote == 1) $normal = $normal + 1; if($vote == 2) $good = $good + 1; $insertvote = $bad."||".$normal."||".$good; $fp = fopen($filename,"w"); fputs($fp,$insertvote); fclose($fp); header("Location: stats.php"); stats.php //the filename $filename = "txt/vote.txt"; //get content of textfile $content = file($filename); $array = explode("||", $content[0]); $bad = $array[0]; $normal = $array[1]; $good = $array[2]; Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 18, 2007 Share Posted October 18, 2007 Each of the radio html must have the same name="some_name" in order for them to be selected correctly and read. <form action="vote.php" method="post"> <table> <tr> <td> <input type="radio" name="poll" value="0"> </td> <td> Bad </td> </tr> <tr> <td> <input type="radio" name="poll" value="1"> </td> <td> Normal </td> </tr> <tr> <td> <input type="radio" name="poll" value="2"> </td> <td> Good </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Vote"> </td> </tr> </table> </form> Quote Link to comment Share on other sites More sharing options...
KahneFan Posted October 18, 2007 Author Share Posted October 18, 2007 I have that, but it will only add to 1 at a time. If you choose "good" and "normal" it will only add to one of them, not both. Thank you for your responses BTW. Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 19, 2007 Share Posted October 19, 2007 A radio button allows you to select only one option out a list. So what's the problem? Maybe you've got it backwards? Quote Link to comment Share on other sites More sharing options...
KahneFan Posted October 19, 2007 Author Share Posted October 19, 2007 ...I changed the "radio" to "checkbox", but it will only register 1 at a time... See above I have changed rado to checkbox to allow multiple selections. It now allows multiple selections, but only one will register when I click "vote" or submit. Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 19, 2007 Share Posted October 19, 2007 Oh! I see where your going, try this: <form action="vote.php" method="post"> <table> <tr> <td> <input type="checkbox" name="poll[]" value="0"> </td> <td> Hamburger </td> </tr> <tr> <td> <input type="checkbox" name="poll[]" value="1"> </td> <td> Hotdog </td> </tr> <tr> <td> <input type="checkbox" name="poll[]" value="2"> </td> <td> Salad </td> </tr> <tr> <td colspan="2"> <input type="submit" value="Vote"> </td> </tr> </table> </form> The variable $poll will be an array automatically, containing the value for whatever was SELECTED. //define an array containing the votes for each type (so you can keep track) $votes = array(0,0,0); //get the values selected as an array $poll = $_POST['poll']; //loop the array (if "hotdog" was selected, one of the array values will be 0, so we just add that index foreach($poll as $p){ $votes[$p]++; } Note, if you want these values to be saved and re-called at a future date, you could save this as a row in a database or read a line from a file, check out the following example: //the below function will take a filename path (IE: somefolder/readme.txt or just readme.txt) //and read line by line and return an array containing the file contents function read_file_array($file_in_question) { $i=0; $file = @fopen($file_in_question,"r") or die("Can't open $file_in_question. It either doesn't exist or there's an internal server error."); while(!feof($file)) { $file_data_array[$i] = fgets($file); $i++; } fclose($file); return $file_data_array; } function write_to_file($file_in_question,$data_to_write) { $file = @fopen($file_in_question,"w") or die("Can't open $file_in_question. It either doesn't exist or there's an internal server error."); @fwrite($file,$data_to_write); fclose($file); } $file_contents = read_file_array("votes.txt"); $votes_string = $file_contents[0];//this is a string format: #|#|#, gotta convert to array //explode(delimiter,string to convert to array), converts a string to an array $votes = explode("|",$votes_string); //get the values selected as an array $poll = $_POST['poll']; //loop the array (if "hotdog" was selected, one of the array values will be 0, so we just add that index foreach($poll as $p){ $votes[$p]++; } //we've added up all the counts, let's rewind and save our data in the text file //we need to convert back into a string so that we can stick it in the file for future reading $votes_string = implode("|",$votes); //run the function (this will clear the file and write the new string) write_to_file("votes.txt",$votes_string); Quote Link to comment Share on other sites More sharing options...
KahneFan Posted October 20, 2007 Author Share Posted October 20, 2007 So, I can add the poll to any page. But, do I need to add your second code to my "vote.php" somehow, or save it as it's own "vote.php"? Also, if I'm not mistaken, your last code is optional(?). Last, it appears as though there is not a PM function on this site, so may I use your email button to send you the link to my test site for you to see what I'm trying to accomplish? Quote Link to comment Share on other sites More sharing options...
kratsg Posted October 20, 2007 Share Posted October 20, 2007 Go ahead. You can email me at kratsg@gmail.com Quote Link to comment Share on other sites More sharing options...
KahneFan Posted October 22, 2007 Author Share Posted October 22, 2007 Thank you very much for the help!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.