我正在尝试在此Smart PHP Poll的同一页面上显示两个民意调查。
我通过使请求同步来显示两个民意调查,但现在每当我单击显示第一个民意调查的结果时,第二个结果而不是第一个显示。我尝试将 divID 菊花链在一起,但我似乎在某个地方出错了,因为它最终只显示“请稍候”而不是显示结果。
html
<script type="text/javascript" src="ajax.js"></script> // in head
<div class ="poll-container">
<div class ="my-poll">
<div id="text"></div>
<SCRIPT LANGUAGE="JavaScript">
getpoll('1377511990', 'text');
</SCRIPT>
</div>
<div class ="my-poll">
<div id="text2"></div>
<SCRIPT LANGUAGE="JavaScript">
getpoll('1377513905', 'text2');
</SCRIPT>
</div>
</div>
ajax 脚本
function createRequestObject() {
var req;
if(window.XMLHttpRequest){
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert('Problem creating the XMLHttpRequest object');
}
return req;
}
var http = createRequestObject();
http.ontimeout = function () {
console.error("The request for " + url + " timed out.");
};
function getpoll(pID, divID) {
var xmlhttp = http;
xmlhttp.open('post', 'fetch.php?action=showpoll&id='+pid, false);
// xmlhttp.open('post', 'fetch.php?action=showpoll&id='+pid+'&divID='+divID, false);
document.getElementById(elementID).innerHTML = "Please wait";
xmlhttp.onreadystatechange = handleResponse;
xmlhttp.send(null);
}
function begen(j){
window.self.document.getElementById("option").value=j;
}
function fetch(){
var xmlhttp = http;
var op=encodeURI(document.getElementById("option").value);
xmlhttp.open('post', 'fetch.php?action=post_result&id='+pid+'&option='+op, false);
// xmlhttp.open('post', 'fetch.php?action=post_result&id='+pid+'&option='+op+'&divID='+divID, false);
xmlhttp.onreadystatechange = handleResponse;
// xmlhttp.onreadystatechange = handleResponse(divID);
xmlhttp.send(null);
}
function result(){
var xmlhttp = http;
xmlhttp.open('post', 'fetch.php?action=see_result&id='+pid, false);
// xmlhttp.open('post', 'fetch.php?action=see_result&id='+pid+'&divID='+divID, false);
xmlhttp.onreadystatechange = handleResponse;
// xmlhttp.onreadystatechange = handleResponse(divID);
xmlhttp.send(null);
}
function handleResponse() {
var xmlhttp = http;
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
var response = http.responseText;
if(response) {
document.getElementById("text").innerHTML = response;
//document.getElementById(divID).innerHTML = response;
}
}
}
获取.php
<?php
foreach($_GET AS $key => $value) {
${$key} = $value;
}
foreach($_POST AS $key => $value) {
${$key} = $value;
}
include ("conf.php"); // database details.
$db=mysql_connect($db_host,$database_user,$database_pass) or
die("<b>MySQL Error:</b> Unable to connect to database please check that you have provided the correct
<li>Database Login username<li>Database Login Password");
//Connect to database or give error if failed
mysql_select_db($db_name,$db)or
die("<b>MySQL Error:</b> Unable to select database please check that you have provided the correct <li>Database name");
// start showpoll action
if($action=="showpoll" && $id!="" && $divID !=""){
$mpi=mysql_query("SELECT * FROM smp_cust");
$col=mysql_fetch_array($mpi);
$pc=mysql_query("SELECT * FROM smp_quiz WHERE tm='$id'");
if(mysql_num_rows($pc)==1){
$now=mysql_fetch_array($pc);
echo "<form onsubmit=\"javascript: return false;\"><table width=\"$col[pw]\" style='border:$col[boc] 1px solid;'
cellpadding=0 cellspacing=0 bgcolor=\"$col[bbc]\"><TR bgcolor=\"$col[hlc]\"
height=\"25\"><TD style='padding:4px;font-size:$col[hls]px;color:$col[ttc];'
valign=middle><B>$now[title]</B></TD></TR><TR height=10><td>
<img src='images/space.gif' height=1 width=1></td></TR>";
$nx=mysql_query("SELECT * FROM smp_answer WHERE tm='$id'");
while($row=mysql_fetch_array($nx)){
echo "<TR height=\"25\"><td style='padding:2px;font:$col[bts]px verdana, tahoma; color:$col[btc];
text-align:left;' valign=top><input type=radio name=note value=\"$row[qid]\"
onclick=\"begen($row[qid]);\"> $row[answer]</td></TR>";
}
echo "<input type=hidden name=\"option\" id=\"option\" value=\"\">";
echo "<TR height=\"25\"><TD align=\"center\">
<input type=\"submit\"
onclick=\"javascript:fetch($id , $divID);\" style='color:$col[buc]' value=\"Submit\"><BR><BR>
<a href=\"#showr\" onclick=\"javascript:result($id, $divID);\">
<font size='1' color=\"$col[btc]\">See Results</a></font>
</TD></TR><TR height=10><td><img src='images/space.gif' height=1 width=1></td></TR>
</Table><BR><table border=0 cellspacing=0 width=\"$col[pw]\">
<TR height=\"25\">
<td width='100%' style='padding:2px;font:10px verdana, tahoma; color:#330000; text-align:right;'
valign=top></a></TD></TR>
</table>";
echo "</form>";
}else{
//echo "<table border=0 cellspacing=0 width=\"$col[pw]\"><TR height=\"25\">
//<td width='100%' style='padding:2px;font:10px verdana, tahoma; color:#330000; text-align:center;'
//valign=top>This poll has been deleted.</td></TR><TR height=\"25\"><td width='100%'
//style='padding:2px;font:10px verdana, tahoma; color:#330000; text-align:center;'
//valign=top>Powered By: <a href=\"http://www.scriptsez.net\" target=\"_blank\">
//<font size='1' color='#330000'>Smart PHP Poll</a></TD></TR></table>";
}
} // end showpoll action
// start post_result action
if($action=="post_result" && $id!=""){
$ip=getenv('remote_addr');
//$ip=rand(1,1000);
$option=trim($option);
$mpi=mysql_query("SELECT * FROM smp_cust");
$col=mysql_fetch_array($mpi);
$pc=mysql_query("SELECT * FROM smp_quiz WHERE tm='$id'");
if(mysql_num_rows($pc)==1){
$now=mysql_fetch_array($pc);
echo "<table width=\"$col[pw]\" style='border:$col[boc] 1px solid;' cellpadding=0 cellspacing=0
bgcolor=\"$col[bbc]\"><TR bgcolor=\"$col[hlc]\" height=\"25\">
<TD style='padding:4px;font-size:$col[hls]px;color:$col[ttc];' valign=middle><B>$now[title]</B>
</TD></TR><TR height=10><td><img src='images/space.gif' height=1 width=1></td></TR>";
$prog=mysql_query("SELECT * FROM smp_result WHERE tm='$id' AND ip='$ip'");
if(mysql_num_rows($prog)>0){
echo "<TR height=\"25\"><td style='padding:2px;font:$col[bts]px verdana, tahoma; color:$col[btc];
text-align:left;' valign=top><b>You have already voted</b></td></TR>";
}else{
if($option!=""){
$ins=mysql_query("INSERT INTO smp_result SET point='$option',ip='$ip', tm='$id'");
}else{
echo "<TR height=\"25\"><td style='padding:2px;font:$col[bts]px verdana, tahoma; color:$col[btc];
text-align:left;' valign=top><b>Please select an option.</b></td></TR>";
}
}
$nx=mysql_query("SELECT * FROM smp_answer WHERE tm='$id'");
$total_results=mysql_query("SELECT * FROM smp_result WHERE tm='$id'");
$total_re=mysql_num_rows($total_results);
while($row=mysql_fetch_array($nx)){
$opt=$row['qid'];
$prong=mysql_query("SELECT * FROM smp_result WHERE tm='$id' AND point='$row[qid]'");
$tse=mysql_num_rows($prong);
$result[$opt]=$tse;
if($total_re>0){
$percentage=round(($tse/$total_re)*100,2);
}else{
$percentage=0;
}
$wt=100-$percentage;
if($percentage==0){
$tdw=1;
}else{
$tdw=$percentage;
}
echo "<TR height=\"18\"><td style='padding:2px;font:$col[bts]px verdana, tahoma;
color:$col[btc]; text-align:left;' valign=top>$row[answer]</td></TR>";
echo "<TR height=\"18\"><td style=\"padding-left:2px;\">
<table width=\"80%\" align=left cellspacing=0 cellpadding=0><tr height=\"10\">
<td bgcolor=\"$col[hlc]\" width=\"$tdw%\"><img src=\"images/spacer.gif\"
width=\"0\" height=\"1\"></td><td width=\"$wt\"
style=\"font:$col[bts]px verdana, tahoma;
color:$col[btc]; text-align:left;\">
<img src=\"images/spacer.gif\" width=\"1\" height=\"1\">$percentage%</td>
</tr></table></td></TR>";
echo "<tr height=\"10\"><td width=100%><img src=\"images/spacer.gif\" width=\"1\"
height=\"1\"></td></tr>";
}
echo "<tr height=\"5\"><td width=100%>
<img src=\"images/spacer.gif\" width=\"1\" height=\"1\"></td></tr>";
echo "<TR height=\"18\"><td style='padding:2px;font:$col[bts]px verdana, tahoma; color:$col[btc];
text-align:left;' valign=top>Total Votes: $total_re</td></TR>";
//echo "<TR height=10><td><img src='images/space.gif' height=1 width=1></td>
//</TR></Table><BR><table border=0 cellspacing=0 width=\"$col[pw]\"><TR height=\"25\">
//<td width='100%' style='padding:2px;font:10px verdana, tahoma; color:#330000; text-align:right;'
//valign=top>Powered By: <a href=\"http://www.scriptsez.net\" target=\"_blank\">
//<font size='1' color='#330000'>Smart PHP Poll</a></TD></TR></table>";
}
}// end action post_result
// start action see_result
if($action=="see_result" && $id!=""){
//$option=trim($option);
$mpi=mysql_query("SELECT * FROM smp_cust");
$col=mysql_fetch_array($mpi);
$pc=mysql_query("SELECT * FROM smp_quiz WHERE tm='$id'");
if(mysql_num_rows($pc)==1){
$now=mysql_fetch_array($pc);
echo "<table width=\"$col[pw]\" style='border:$col[boc] 1px solid;'
cellpadding=0 cellspacing=0 bgcolor=\"$col[bbc]\"><TR bgcolor=\"$col[hlc]\"
height=\"25\"><TD style='padding:4px;font-size:$col[hls]px;color:$col[ttc];'
valign=middle><B>$now[title]</B></TD></TR><TR height=10><td>
<img src='images/space.gif' height=1 width=1></td></TR>";
$nx=mysql_query("SELECT * FROM smp_answer WHERE tm='$id'");
$total_results=mysql_query("SELECT * FROM smp_result WHERE tm='$id'");
$total_re=mysql_num_rows($total_results);
while($row=mysql_fetch_array($nx)){
$opt=$row['qid'];
$prong=mysql_query("SELECT * FROM smp_result WHERE tm='$id' AND point='$row[qid]'");
$tse=mysql_num_rows($prong);
$result[$opt]=$tse;
if($total_re>0){
$percentage=round(($tse/$total_re)*100,2);
}else{
$percentage=0;
}
if($percentage==0){
$tdw=1;
}else{
$tdw=$percentage;
}
$wt=100-$percentage;
echo "<TR height=\"18\"><td style='padding:2px;font:$col[bts]px verdana, tahoma;
color:$col[btc]; text-align:left;' valign=top>$row[answer]</td></TR>";
echo "<TR height=\"18\"><td style=\"padding-left:2px;\"><table width=\"80%\"
align=left cellspacing=0 cellpadding=0><tr height=\"10\"><td bgcolor=\"$col[hlc]\"
width=\"$tdw%\"><img src=\"images/spacer.gif\" width=\"0\" height=\"1\">
</td><td width=\"$wt\" style=\"font:$col[bts]px verdana, tahoma; color:$col[btc];
text-align:left;\"><img src=\"images/spacer.gif\" width=\"1\" height=\"1\">$percentage%
</td></tr></table></td></TR>";
echo "<tr height=\"10\"><td width=100%><img src=\"images/spacer.gif\"
width=\"1\" height=\"1\"></td></tr>";
}
echo "<tr height=\"5\"><td width=100%><img src=\"images/spacer.gif\" width=\"1\"
height=\"1\"></td></tr>";
echo "<TR height=\"18\"><td style='padding:2px;font:$col[bts]px verdana, tahoma; color:$col[btc];
text-align:left;' valign=top>Total Votes: $total_re</td></TR>";
//echo "<TR height=10><td><img src='images/space.gif' height=1 width=1></td></TR>
//</Table><BR><table border=0 cellspacing=0 width=\"$col[pw]\"><TR height=\"25\">
//<td width='100%' style='padding:2px;font:10px verdana, tahoma; color:#330000; text-align:right;'
//valign=top>Powered By: <a href=\"http://www.scriptsez.net\" target=\"_blank\">
//<font size='1' color='#330000'>Smart PHP Poll</a></TD></TR></table>";
}
}// end action see_result
?>