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!