Ajax/PHP poll - problem

Hi,

So I have a poll which actually works as a Facebook tab. It works in the sense that it posts the choices of the user into a text file, it doesn’t work in returning the ajax message. The thing is, if I remove a couple of lines from the code it will actually return the echo of the “action” .php without leaving the FB tab page… however it doesn’t recognise which radio button choice was selected. So in other words it just posts option A only, even if you select option B. Then, with the queryParams in place the correct data is posted but the whole ajax part fails to work and it actually redirects to the .php file which of course is a blank page with the text “Test”.

Any ideas?

The Facebook tab “FBML” is as follows:

<style type="text/css">
#poll {
	width: 100%;
	height: 200px;
	background: #ccc;
}
</style>
<div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form action="http://www.mydomain.com/poll_vote.php" method="post">
Yes:
<input type="radio" name="vote" value="0" />
<br />No:
<input type="radio" name="vote" value="1" />
<button type="submit" onclick="return submitAjaxForm();" value="" class="button" />
<p id="ajaxMessage"></p>
</form>
</div>
<script><!--
function submitAjaxForm() 
{ 
	// declare a new FBJS AJAX object
	var ajax = new Ajax(); 
	ajax.responseType = Ajax.FBML;
 
	// define a callback to handle the response from the server
	ajax.ondone = function(data) 
	{ 
		document.getElementById('ajaxMessage').setInnerFBML(data);
	}
 
	// collect field values
	var queryParams = { 'vote' : document.getElementByName('vote').getValue() };
	ajax.post("http://www.mydomain.com/poll_vote.php", queryParams);
	return false;
}
//--></script>

Then poll_vote.php:

<?php
$vote = $_REQUEST['vote'];

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

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

if ($vote == 0)
  {
  $yes = $yes + 1;
  }
if ($vote == 1)
  {
  $no = $no + 1;
  }

//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);

echo "Test";
?>

Now, if this part:


	var queryParams = { 'vote' : document.getElementByName('vote').getValue() };
	ajax.post("http://www.mydomain.com/poll_vote.php", queryParams);
	return false;

I change into:


	ajax.post("http://www.mydomain.com/poll_vote.php");
	return false;

Then the ajax part works dandy and the person stays on the Facebook tab page, they just get the “Test” text under the poll. It does also post data into the .txt file like this, but it doesn’t differentiate whether the person choose yes or no.

Odd?

I have this exact same system working for a contact form, of course the “action”.php is different.

Thanks in advance!

Anyone have any clue? I’m still wrestling with this problem.

If I have simply this at the end of the fbml…

ajax.post("http://www.mydomain.com/poll_vote.php");
	return false;

… the poll graphic (with correct results) actually loads with ajax into the tab. However no matter whether you choose yes or no it will add +1 to yes.

If I use this…

ajax.post("http://www.mydomain.com/poll_vote.php[COLOR="Red"]?vote="+int[/COLOR]);
	return false;

… the votes are added correctly, but the submit causes a redirect to the .php file and the ajax doesn’t load it into the tab.

:rolleyes:

It seems like I am SO close, but I can’t figure it out. Any ideas?

EDIT: In my initial post I have the echo “Test”, but in reality it is the code below:


<h2>Result:</h2>
<table>
<tr>
    <td>Yes:</td>
    <td><div style="background: url('http://www.mydomain.com/poll.gif'); width: <?php echo(100*round($yes/($no+$yes),2)); ?>px; height: 20px;"></div></td>
    <td><p style="text-align: center; font-size: 12px;"><?php echo(100*round($yes/($no+$yes),2)); ?>&#37;</p></td>
</tr>
<tr>
	<td>No:</td>
	<td><div style="background: url('http://www.mydomain.com/poll.gif'); width: <?php echo(100*round($no/($no+$yes),2)); ?>px; height: 20px;"></div></td>
	<td><p style="text-align: center; font-size: 12px;"><?php echo(100*round($no/($no+$yes),2)); ?>%</p></td>
</td>
</tr>
</table>

That all loads correctly into the FB tab with ajax if there is no ?vote=+int, but yes/no is not registered. With it, it registers the answer correctly but does not load into the FB tab with ajax but instead directs to the page.