...

View Full Version : Php Poll



Danaldinho
02-15-2008, 06:54 PM
I have this poll on my website now:

http://www.chamillionairechat.com/poll/ajax-poller.html

And I followed these instructions carefully from:

http://www.dhtmlgoodies.com/index.html?whichScript=ajax-poller

But I don't know were I have gone wrong, because something isn't working right

Does anyone know?

Andrew Johnson
02-15-2008, 07:03 PM
I see



".$inf["pollerTitle"].""; // 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 "

".$infOptions["optionText"]."
"; } } ?>

outseeker
02-15-2008, 07:09 PM
First thing that's noticable: You haven't used the full PHP open command. U have a "short open" command which probably won't work unless PHP is configured to accept it (which it isnt by default, and by the looks as it outputs it as text when you right click and "view source")

For example, you can see this which should have been executed, not displayed.

<?

require_once("dbConnect.php");

?>

They wanna read <?PHP instead of <?. The close tags ?> are ok.

If that in itself doesn't fix it, post again and we can check it out. The original code would be good, as when I hit View Source I can't see all of the code involved.

Danaldinho
02-15-2008, 10:40 PM
That didn't fix it :(

And here is the full code:


<?PHP

require_once("dbconnect.php");

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<?
@include($_SERVER['DOCUMENT_ROOT']."/config/metatags.inc");
?>
<title>Ajax poller</title>
<style type="text/css">



/* DEMO CSS */
img{
border:0px;
}

#mainContainer{
width:760px;
margin:0 auto;
text-align:left;
background-color:#FFF;

}

#mainContent{
padding:10px;
}

.clear{
clear:both;
}
</style>
<link rel="stylesheet" href="css/ajax-poller.css" type="text/css">
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/ajax-poller.js"> </script>

</HEAD>
<BODY>

<form action="<? echo $_SERVER['PHP_SELF']; ?>" onsubmit="return false" method="post">
<div id="mainContainer">
<div id="header"><img src="/images/heading3.gif"></div>
<div id="mainContent">

<?
$pollerId = 3; // Id of poller
?>
<!-- START OF POLLER -->
<div class="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>
<div class="poller_waitMessage" id="poller_waitMessage<? echo $pollerId; ?>">
Getting poll results. Please wait...
</div>
<div class="poller_results" id="poller_results<? echo $pollerId; ?>">
<!-- This div will be filled from Ajax, so leave it empty --></div>
</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

}
</script>

<p>This is an example of a poll script. It uses Ajax(Asyncron Javascript And XML) to send your vote to the server. Ajax is also used to return the results from this poll to your browser.</p>
<p>In this demo, I haven't limited the number of votes per user. This is something you can implement by setting the Javascript variable
useCookiesToRememberCastedVotes to true in the script or by implementing your own method.</p>
</div>
<div class="clear"></div>
</div>
</form>

</BODY>
</HTML>

Andrew Johnson
02-15-2008, 10:51 PM
You need to name your PHP files with a .PHP extension, not a .HTML extension.

outseeker
02-16-2008, 03:54 AM
Got it sorted for ya mate. Good call about the extension Andrew. Unless you can edit the PHP.INI file for your server and add .html as a php executable extension you need to rename your ajax-poller.html to .php. Now it will just show the vote button and no poll. Because by default $pollerId=3. There is only one poll in the demo database ;) so $pollerId=1;

That worked fine for me! :D (once I remembered to execute createDbTables.php after editing it for dbname pass etc.) it's running at http://outer.reaches.dyndns.org/poller/ajax-poller.php till you say u got yours working.

My previous suggestion of short open tags is irrelevant by the way. Their demo code worked ok without any changes there. Oh yeah, don't forget clickin here if ur a happy chappy! :D http://www.codingforums.com/images/buttons/reputation.gifhttp://www.codingforums.com/reputation.php?p=656586 (http://www.codingforums.com/reputation.php?p=656586)

Danaldinho
02-16-2008, 12:35 PM
Ok I fixed that now, thanks so much :)

But outseeker how do you add the options for a user to vote?

I can't see were to do it :(

Thanks

Danaldinho
02-17-2008, 07:44 PM
Anyone? :(

outseeker
02-17-2008, 07:50 PM
They are all in the MySQL Database you have set up mate. I recommend www.sql-front.com for easily connecting and manipulating these. Same details u put in the config file for username and password. Too easy ;)

Danaldinho
02-17-2008, 07:59 PM
I don't understand though

I have gone to poll and then admin and made a new poll

But it wont show in the box on http://www.chamillionairechat.com/poll/ajax-poller.php

I don't know what I am doing wrong and different to what you have done

outseeker
02-17-2008, 10:26 PM
You mustn't have the correct details for username, password and database in your files. Edit them and ensure all details are correct. If they're correct, running the php file that installs tables will do it's job, and the admin will add polls.

outseeker
02-18-2008, 06:44 AM
Did you get it to work for you mate?

Danaldinho
02-18-2008, 10:32 AM
Nope :(

The passwords are correct

What do mean run the php file that installs the tables?

Do you mean this: createDbTables.php

If so, how do you run it?

Do you mean just type it in your browser and click Go?

outseeker
02-19-2008, 12:01 AM
Yeah have you edited createDbTables.php? It wants your login and database details manually entered itself just like the admin file and the index file. If you run createdbtables.php with correct username etc it will add the database entries for the poller. Just put it's address in the address bar, the same way you are viewing your website. Like yourwebsite.com/poller/createdbtables.php. Then view the poller. It worked fine for me. Have you got a link for us dude? You seem to be havin a lot of trouble, but I have no link for me to play with and discover the fault :( ehehe it worked just fine on my system! You're not double clicking the createdbtables in your windows explorer or something are u? hehe it does need to be on the live server ;)

Danaldinho
02-19-2008, 04:13 PM
Here is a link: http://www.chamillionairechat.com/poll/createDbTables.php

:(

outseeker
02-19-2008, 08:49 PM
Dan, I'm sure I mentioned to change in your ajax-poller.php:


$pollerId = 3;

to:


$pollerId = 1;

Your current code is looking for the third poll in the database. There is only one demo poll in the database man!! Change that, and I don't see why it wouldn't work (If u haven't pmd me back yet) :)

lol and stop sad facing! It will be ok mate! :D

Danaldinho
02-20-2008, 05:45 PM
I've done that now

Still no luck :(

Danaldinho
02-20-2008, 06:19 PM
Oh hang on mate, its working :)

Thanks so much :)

woooo

Just two last questions:

Do you know how to make it so only one user can vote per IP?

And do you know how I can add it on my main front page: http://www.chamillionairechat.com

Underneath the "Poll" header

outseeker
02-21-2008, 02:00 AM
YAY!! :D I knew it would work, we've been over it enough! haha

OK they have some code in the ajax-poller-cast-vote-php.php for one vote per IP, but you need to add your own "finishing touches" to it.

Find:

// Insert new vote into the database
// You may put in some more code here to limit the number of votes the same ip adress could cast.

if($optionId)mysql_query("insert into poller_vote(optionID,ipAddress)values('".$optionId."','".getenv("REMOTE_ADDR")."')");

The code above adds the vote to the database with no checks. At this point, you could check to see if the remote IP exists in the poller_vote(ipAddress) field, if so don't record their vote, else execute the above code. They make mention of setting a JavaScript variable useCookiesToRememberCastedVotes to true, but this just made everyone see the results and no option to vote. You probably want to check their IP against the database, as ppl can just delete their cookies and vote as many times as they like. Or not support the use of cookies in the first place ;) If the IP exists, set var useCookiesToRememberCastedVotes to true, and they will only see the results.

lol I was bangin my head into the screen wondering why my code I was tryin 2 make for u wasnt working.. I was editing a local file, then looking at the live version and seeing no difference... DERRRR XD working on the live file helps!

I should have the code snippet for u shortly chief.

outseeker
02-21-2008, 02:21 AM
Actually, I take it back about the cookie problem.

In ajax-poller.php Find:

<!-- END OF POLLER -->
<script type="text/javascript">
if(useCookiesToRememberCastedVotes){
var cookieValue = Poller_Get_Cookie('dhtmlgoodies_poller_<?php echo $pollerId; ?>');
if(cookieValue && cookieValue.length>0)displayResultsWithoutVoting(<?php echo $pollerId; ?>); // This is the code you can use to prevent someone from casting a vote. You should check on cookie or ip address

}
</script>

Replace With:

<!-- END OF POLLER -->
<script type="text/javascript">
var useCookiesToRememberCastedVotes = 'true';
if(useCookiesToRememberCastedVotes){
var cookieValue = Poller_Get_Cookie('dhtmlgoodies_poller_<?php echo $pollerId; ?>');
if(cookieValue && cookieValue.length>0)displayResultsWithoutVoting(<?php echo $pollerId; ?>); // This is the code you can use to prevent someone from casting a vote. You should check on cookie or ip address

}
</script>


This will only allow one vote per person for the life of the cookie. I thought it was broken and only showing me the results, cuz I'd deleted the records of the votes from the database but still couldn't vote. Naturally the cookie was still doing it's job, so I couldn't vote again lol

Let me know how u go champ :) I've done this mod to my live version, it seems to work no probs. I've removed everyone's votes and voted once myself. Now when I view the page it shows the results only. Try for urself, u should be able to vote the first time u view the poller, then it won't let ya.

outseeker
02-21-2008, 03:04 AM
Well rather than explain it, I will chop it up for ya.

Insert this code into your <head> are in your webpage document:


<?php
require_once("dbConnect.php");
?>
<link rel="stylesheet" href="css/ajax-poller.css" type="text/css">
<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript" src="js/ajax-poller.js"></script>

This should not modify anything about the layout of your page. You may wish to confirm that before continuing.

Then comes the addition to the <body> of the document. Add this wherever you want the actual poller to appear:


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return false" method="post">
<?
$pollerId = 1; // Id of poller
?>
<!-- START OF POLLER -->
<div class="poller">
<div class="poller_question" id="poller_question<?php 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(<?php echo $pollerId; ?>,document.forms[0])"><img src="images/vote_button.gif"></a>
</div>
<div class="poller_waitMessage" id="poller_waitMessage<?php echo $pollerId; ?>">
Getting poll results. Please wait...
</div>
<div class="poller_results" id="poller_results<?php echo $pollerId; ?>">
<!-- This div will be filled from Ajax, so leave it empty --></div>
</div>
<!-- END OF POLLER -->
<script type="text/javascript">
var useCookiesToRememberCastedVotes = 'true';
if(useCookiesToRememberCastedVotes){
var cookieValue = Poller_Get_Cookie('dhtmlgoodies_poller_<?php echo $pollerId; ?>');
if(cookieValue && cookieValue.length>0)displayResultsWithoutVoting(<?php echo $pollerId; ?>); // This is the code you can use to prevent someone from casting a vote. You should check on cookie or ip address
}
</script>
<div class="clear"></div>
</form>

I removed the <style> declarations from the ajax-poller.php file, and added them to the css/ajax-poller.css file. Here's a copy of what I'm using, u can just overwrite your existing ajax-poller.css:


.poller{ /* The poller box */

text-align:left;
float:right;
width:150px;
height:220px;
padding:15px;
border:3px double #317082;
background-color:white;
background-repeat:no-repeat;
background-position: center center;
margin:10px;
}
/* Poller title above radio buttons */
.pollerTitle{
margin-bottom:5px;
font-weight:bold;
}
/* Label for each radio button */
.pollerOption{
margin:0px;
}
/* Label for each option above graph - i.e. results */
.result_pollerOption{
margin:0px;
font-size:0.8em;
}
/* Title of poller - when ajax shows the results */
.result_pollerTitle{
margin-bottom:5px;
font-weight:bold;
font-size:0.9em;
}
/* Don't change this one, it is used to get the left and right image(the corner) positioned correctly */
.result_pollGraph img{
float:left;
}
/* The <div> tag where the percentage result is shown */
.result_pollGraph div{
float:left;
height:16px;
background-repeat:repeat-x;
color:#FFF;
font-size:0.9em;
line-height:16px;
}
/* Graph div - parent of both the corner images and the div displaying percentage votes */
.result_pollGraph{
height:16px;
}

/* Message when Ajax is working getting restults fromt the server */
.poller_waitMessage{
display:none;
}

.result_totalVotes{
clear:both;
font-size:0.8em;
margin-top:10px;
font-style:italic;
}

I have a page running with just the plain poller for you to see at http://outer.reaches.dyndns.org/poller/ajax-poller2.php - It's just bare bones, no images or coloration etc. Their demo had the entire <body> as light blue and stuff, I figured u wouldn't want that modifying your whole page. Let me know if u have any problems inserting it. Maybe make a backup of your page first, in case it is not quite right you can just copy your old one back over while u investigate. :)

outseeker
02-21-2008, 03:12 AM
Oh yeah man, maybe don't use an IFRAME. The point of using AJAX in this poller is that it doesn't have to reload the whole page to cast a vote ;) Using an IFRAME to embed the AJAX poller kinda defeats the purpose of using AJAX hehe

If you do wanna use an IFRAME, I spose it's all good. Just at the moment your IFRAME is looking for poll.php which doesn't exist ;) U can try copying all the code I gave u above and just pasting it into an empty html document for the IFRAME to read from. I figure it should work ok :) As always, keep me posted

Danaldinho
02-21-2008, 01:24 PM
The thing you gotta remember is aswell that I can't add this line:


<link rel="stylesheet" href="css/ajax-poller.css" type="text/css">

Because I already have one of those:


<link rel="stylesheet" type="text/css" href="/style.css" />

And it doesn't seem to be working and I have done exactly what you said: http://www.chamillionairechat.com

Do you know whats up?

outseeker
02-22-2008, 12:57 AM
Nah brother, u can have multiple style sheets in the header, the ajax-poller.css only controls the poller's attributes. It shouldn't affect anything else on the page. ahh, and it doesn't display properly because your website's index file is a .html not a .php! Sorry mate, bad call on my part. Looks like you will have 2 use an IFRAME after all :( Unless you can rename your index file to .php? That should have it working "as is". Simplest test is copy your index.htm file to index.php and try to view it on the server. I'm quite sure the poller will then work. If it does, it's up to you what u wanna do.

Sorry not to be spot on first time mate! Unless u wanna rename ur index file from .htm to .php, IFRAME it back up again with src=poller.php (Just make a new file poller.php with the code above) and that should be fine. Sorry again to have you add the code directly to the page, when way early on in the testing of this code we found it wouldn't execute when not named .php! I blame lack of sleep hehe

We are so nearing completion Dan!! :D

Danaldinho
02-22-2008, 11:14 AM
Ok have a look now on my website: http://www.chamillionairechat.com

There is still some error :(

But if you think it is because it's a .html file, i did change it to a index.php file and got these errors on the main page:


Warning: require_once(/poll/dbConnect.php) [function.require-once]: failed to open stream: No such file or directory in /home/dannym/public_html/chamillionairechat.com/index.php on line 2

Fatal error: require_once() [function.require]: Failed opening required '/poll/dbConnect.php' (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/dannym/public_html/chamillionairechat.com/index.php on line 2

So thats why I changed it back

outseeker
02-23-2008, 09:23 AM
>.< Let's try and finish this thing!

First up, remove anything to do with the poller from your main html. This includes the references to ajax-poller.js and .css. Because of this poller wanting to be called .php there is no point having any of it's data in the main page since we have been forced to use an IFRAME. Consider an IFRAME a seperate page, even though it is embedded.

So just make ur main page your clean html, with an IFRAME pointing to poll/poller.php.

Now once you have cleaned that back up, we need to sort out your poller.php file.

Because it is in an IFRAME it is treated like a seperate web page. This means you need to add standard html headers to the poller.php like you have on your main page. Look at the example ajax-poller.php we had working. Your poller.php has to have headers, <head><body> etc just like that in order to run.

Make sure your IFRAME SRC is "poll/poller.php" so the dbconnect.php is in the same directory as the poller.php you are executing. Then include_once(dbconnect.php) will work.

Danaldinho
02-23-2008, 09:35 AM
Ok done that mate

But as you see, its gone cocked up again :( - http://www.chamillionairechat.com/poller.php

outseeker
02-23-2008, 09:45 AM
I'm watching u edit this live now, hurry up! :P

Dont use /poller.php Dan, use poll/poller.php!!! Make sure the include_once just says include_once(dbconnect.php) cuz its in same directory

Danaldinho
02-23-2008, 09:51 AM
Ok, done mate :)

outseeker
02-23-2008, 09:51 AM
<PM to invite to chat> this is just tooo slow going! ;)

Danaldinho
02-23-2008, 11:32 AM
Ok, this is now completed after a long time :p

But seriously outseeker you have been a big help to me and I couldn't of done this without you

I appreciate all the time you took to help me and I am really grateful

So thanks again mate :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum