Jump to content

Vote poll problem


regoch

Recommended Posts

I have problem with code for vote poll.

When my header is

1. <html>

<head>

<title>Smart PHP Poll </title>

</head>

 

everything working fine

 

2.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<title>Smart PHP Poll </title>

</head>

 

won't work. I suppose that is something in form for voting that is not compatible with: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

my code is:

<?php
foreach($_GET AS $key => $value) {
${$key} = $value;
} 
foreach($_POST AS $key => $value) {
${$key} = $value;
} 
session_start();

//Podaci za spajanje na bazu podataka
require_once('../../admin/config.php');

//Array za provjeru jesu li ispunjena sva polja
$errmsg_arr = array();

//Isključivanje provjere jesu li ispunjena sva polja
$errflag = false;

//Spajanje na mysql server
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if(!$link) {
	die('Greška prilikom spajanja na server: ' . mysql_error());
}

//Odabir baze podataka
$db = mysql_select_db(DB_DATABASE);
if(!$db) {
	die("Greška prilikom spajanja na bazu podataka");
}
if($action=="showpoll" && $id!=""){
$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'><strong>$now[title]</strong></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' id='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);' style='color:$col[buc]' value='Submit'></br></br><a href='#showr' onclick='javascript:result($id);'><font size='1' color='$col[btc]'>Pogledaj rezultate</a></font></td></tr><tr height='10'><td><img src='images/space.gif' height='1' width='1'></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></table>";
}
}
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'><strong>$now[title]</strong></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'><strong>You have already voted</strong></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'><strong>Please select an option.</strong></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>";
}
}
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'><strong>$now[title]</strong></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>";
}
}
?>

Link to comment
Share on other sites

I have problem with code for vote poll.

 

First, can you describe what the problem is? Are there any errors being displayed? If not, did you check the error log for PHP? Did you ensure that either one of display_errors or log_errors is configured to work on your server, if you are administrating it yourself. If nothing is showing up in the page in the the source HTML, you don't have PHP displaying errors and need to check your log. I've seen them called named differently depending on your host and/or PHP configuration. Usually php_errors or php_errors.log

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Smart PHP Poll </title>
</head>

won't work.

 

This shouldn't be affecting your PHP output. This is a doctype declaration telling the client what type of document it should be expecting to read. This is valid and I'm almost certain that this isn't your problem.

 

my code is: *snip*

 

This code is fine syntactically, so I'm guessing you have a problem in another file. If you can check your error log or paste any error message you get that would probably be oodles of help in helping you.

Link to comment
Share on other sites

Ups, my bad!

Error is that that when I have just <html> voting it's work normal, count the vote and put it in database. When i change to "

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head>

it won't count vote in database, just telling me that i select no option to vote.

Sorry about english, I hope you understand the problem.

 

You can see it online http://www.epakostane.com/index.php

Page is under consturction!

Link to comment
Share on other sites

This was not a PHP problem, it was an AJAX/Javascript problem.

 

The original javascript problem was as follows:


*snip*

function begen(j) {
    window.self.document.all.option.value = j;
}

function fetch(pid) {
    var op = encodeURI(document.getElementById("option").value);
    http.open('post', 'http://example.com/include/anketa/fetch.php?action=post_result&id=' + pid + '&option=' + op);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

*snip*

 

in the poll, each radio button had onClick="begen(j)", j being the value of the option selected. In the original code the function begen() does nothing important. The solution was to have the function set a value in the javascript to be sent to the backing PHP script.

 

The new code is as follows:

*snip*
var op; //add variable with no value for use in fetch()

function begen(j) {
    return op = j; //return op so it can be sent with fetch();
}

function fetch(pid) {
    //var op = encodeURI(document.getElementById("option").value);
    http.open('post', 'http://www.example.com/include/anketa/fetch.php?action=post_result&id=' + pid + '&option=' + op);
    http.onreadystatechange = handleResponse;
    http.send(null);
}
*snip*

 

This solved the problem.

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.