Jump to content

PHP & AJAX (flat) Poll


KahneFan

Recommended Posts

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(?).

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.