ankieta wykorzystuje 2 pliki js:
<script type="text/javascript" src="js/ajax-poller.js"> </script>
ajax.js to po prostu funkcje "aktywujące" ajax, natomiast
ajax-poller.js jest strasznie dlugie wiec przytavczam tylko te najwazniejsze funkcje:
function showVoteResults(pollId,ajaxIndex)
{
document.getElementById('poller_waitMessage' + pollId).style.display='none';
var xml = ajaxObjects[ajaxIndex].response;
xml = xml.replace(/\n/gi,'');
var reg = new RegExp("^.*?<pollerTitle>(.*?)<.*$","gi");
var pollerTitle = xml.replace(reg,'$1');
var resultDiv = document.getElementById('poller_results' + pollId);
var titleP = document.createElement('P');
titleP.className='result_pollerTitle';
titleP.innerHTML = pollerTitle;
resultDiv.appendChild(titleP);
var options = xml.split(/<option>/gi);
pollVotes[pollId] = new Array();
totalVotes[pollId] = 0;
for(var no=1;no<options.length;no++){
var elements = options[no].split(/</gi);
var currentOptionId = false;
for(var no2=0;no2<elements.length;no2++){
if(elements[no2].substring(0,1)!='/'){
var key = elements[no2].replace(/^(.*?)>.*$/gi,'$1');
var value = elements[no2].replace(/^.*?>(.*)$/gi,'$1');
if(key.indexOf('optionText')>=0){
var pOption = document.createElement('P');
pOption.className='result_pollerOption';
pOption.innerHTML = value;
resultDiv.appendChild(pOption);
}
if(key.indexOf('optionId')>=0){
currentOptionId = value/1;
}
if(key.indexOf('votes')>=0){
var voteDiv = document.createElement('DIV');
voteDiv.className='result_pollGraph';
resultDiv.appendChild(voteDiv);
var leftImage = document.createElement('IMG');
leftImage.src = voteLeftImage;
voteDiv.appendChild(leftImage);
var numberDiv = document.createElement('DIV');
numberDiv.style.backgroundImage = 'url('' + voteCenterImage + '')';
numberDiv.innerHTML = '0%';
numberDiv.id = 'result_voteTxt' + currentOptionId;
voteDiv.appendChild(numberDiv);
var rightImage = document.createElement('IMG');
rightImage.src = voteRightImage;
voteDiv.appendChild(rightImage);
pollVotes[pollId][currentOptionId] = value;
totalVotes[pollId] = totalVotes[pollId]/1 + value/1;
}
}
}
}
var totalVoteP = document.createElement('P');
totalVoteP.className = 'result_totalVotes';
totalVoteP.innerHTML = txt_totalVotes + totalVotes[pollId];
voteDiv.appendChild(totalVoteP);
setPercentageVotes(pollId);
slideVotes(pollId,0);
}
function prepareForPollResults(pollId)
{
document.getElementById('poller_waitMessage' + pollId).style.display='block';
document.getElementById('poller_question' + pollId).style.display='none';
}
function castMyVote(pollId,formObj)
{
var elements = formObj.elements['vote[' + pollId + ']'];
var optionId = false;
for(var no=0;no<elements.length;no++){
if(elements[no].checked)optionId = elements[no].value;
}
Poller_Set_Cookie('dhtmlgoodies_poller_' + pollId,'1',6000000);
if(optionId){
var ajaxIndex = ajaxObjects.length;
ajaxObjects[ajaxIndex] = new sack();
ajaxObjects[ajaxIndex].requestFile = serverSideFile + '?pollId=' + pollId + '&optionId=' + optionId;
prepareForPollResults(pollId);
ajaxObjects[ajaxIndex].onCompletion = function(){ showVoteResults(pollId,ajaxIndex); }; // Specify function that will be executed after file has been found
ajaxObjects[ajaxIndex].runAJAX(); // Execute AJAX function
}
}
Zresztą... ta ankieta to ogolnie dostępny skrypt ankiety ajax dostępny na stronie:
http://www.dhtmlgoodies.com/index.html?whi...ipt=ajax-pollerSam skrypt po dostosowaniu pod moje opcje działa pięknie. PRzestaje gdy dodaje przed nim dowolny inny formularz :/
Będę wdzięczny za wszelką pomoc!
Pozdrawiam,
Olek
z racji na ograniczoną ilość tekstu w poscie tutaj podaje kod formularza ankiety w html:
<form action="<? echo $_SERVER['PHP_SELF']; ?>" onsubmit="return false" method="post" >
<?
$pollerId = 1; // Id of poller
?>
<!-- START OF POLLER -->
<div class="poller_question" id="poller_question<? echo $pollerId; ?>">
<?
// Retreving poll from database
$res = mysql_query("select * from poller where ID='$pollerId'");
if($inf = mysql_fetch_array($res)){
echo "<p class=\"pollerTitle\">".$inf["pollerTitle"]."
</p>"; // Output poller title
$resOptions = mysql_query("select * from poller_option where pollerID='$pollerId' order by pollerOrder") or die(mysql_error()); // Find poll options, i.e. radio buttons
while($infOptions = mysql_fetch_array($resOptions)){
if($infOptions["defaultChecked"])$checked=" checked"; else $checked = "";
echo "
<p class=\"pollerOption\"><input$checked type=\"radio\" value=\"".$infOptions["ID"]."\" name=\"vote[".$inf["ID"]."]\" id=\"pollerOption".$infOptions["ID"]."\"><label for=\"pollerOption".$infOptions["ID"]."\" id=\"optionLabel".$infOptions["ID"]."\">".$infOptions["optionText"]."
</label></p>";
}
}
?>
<a href="#" onclick="castMyVote(<? echo $pollerId; ?>,document.forms[0])">
<img src="images/vote_button.gif"></a> <div class="poller_waitMessage" id="poller_waitMessage<? echo $pollerId; ?>">
Pobieranie wyników ankiety. Prosze czekać...
<div class="poller_results" id="poller_results<? echo $pollerId; ?>">
<!-- This div will be filled from Ajax, so leave it empty --></div> <!-- END OF POLLER -->
<script type="text/javascript"> if(useCookiesToRememberCastedVotes){
var cookieValue = Poller_Get_Cookie('dhtmlgoodies_poller_<? echo $pollerId; ?>');
if(cookieValue && cookieValue.length>0)displayResultsWithoutVoting(<? echo $pollerId; ?>); // This is the code you can use to prevent someone from casting a vote. You should check on cookie or ip address
}