PHP/jQuery Poll tutorial - cool if it worked

Hi,

I would like to use this poll, but it doesn’t really work. It seems to in the demo, but the count doesn’t work right and is stuck at 53. I messed around a little and have no luck. It seems to be the PHP and not the jQuery.

The demo is here: http://net.tutsplus.com/javascript-a…query-and-php/

My attempt is here: http://ehydrant.com/indexpoll.html

If anyone has any thoughts, me and about 50 people at the original tut would really appreciate it.

BTW - it sets the IP to a cookie with cookie.jQuery, so after one try, it has to be cleared.

Thanks guys.

What error messages are you getting?

Are you seeing one like this:


[Sun Oct 26 12:34:16 2008] [error] [client xx.xxx.xx.xxx] PHP Warning:
fopen(data/essaycontest_ips.txt.lock) [function.fopen]: failed to open stream: 
Permission denied in /var/www/html/scripts/poll/flatfile.php on line 181, referer:
http://www.example.com/scripts/poll/poll.html

(taken from that tut page)

What changes have you made to the php file. The ajax request is being made correctly, but the return is an empty array.

These appear to be the lines that are involved most directly in your situation.


$rows = $db->selectWhere(VOTE_DB, new SimpleWhereClause(OPT_ID, "!=", 0), -1, new OrderBy(OPT_VOTES, DESCENDING, INTEGER_COMPARISON));  
print json_encode($rows);  

have you checked your database to see if the voting results are being added to it?

I don’t know PHP at all, but poll.js is supposed to write to two files: votes.txt and ips.txt. It does write to ips.txt which has about 10 IP addresses in it right now. The votes.txt file is empty. I’m guessing the PHP that is supposed to increment the vote is wrong or broken and the tutorial writer never checked it.

I don’t get any error messages ever. it just shows

Poll Results

Total Votes: 0

If I had left the default votes.txt file it would show 53 votes like everyone else commenting on the tutorial has - apparently a starter number.

It seems like a simple problem, but lots of people who tried the tut are stumped. :injured:

Can you please package together the files that you have that relate to this voting poll, and attach them to a message so that we can dig in to them and sort this out.

My demo (just a stripped version) is attached as a zip.

Anyone?

The attachment is still pending approval. I’ll try to find out what’s going on.

The attachment is validated. Go nuts :slight_smile:

Okay, it seems that there are many issues with the php code.
Here’s the list of errors and warnings that I get when running it as a non-scripted submit.

Notice: Use of undefined constant INT_COL - assumed 'INT_COL' in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 47
Notice: Use of undefined constant DATE_COL - assumed 'DATE_COL' in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 48
Notice: Use of undefined constant STRING_COL - assumed 'STRING_COL' in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 49
Notice: Use of undefined constant FLOAT_COL - assumed 'FLOAT_COL' in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 50
Notice: Undefined index: poll in /home/amemoryt/public_html/beta/test/sitepoint/poll/poll.php on line 23
Notice: Undefined index: poll in /home/amemoryt/public_html/beta/test/sitepoint/poll/poll.php on line 71
Warning: fopen(data/ips.txt.lock) [function.fopen]: failed to open stream: Permission denied in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 183
Warning: flock() expects parameter 1 to be resource, boolean given in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 184
Warning: fwrite(): supplied argument is not a valid stream resource in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 355
Warning: fclose(): supplied argument is not a valid stream resource in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 356
Warning: flock() expects parameter 1 to be resource, boolean given in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 196
Warning: Cannot modify header information - headers already sent by (output started at /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php:47) in /home/amemoryt/public_html/beta/test/sitepoint/poll/poll.php on line 94
Warning: fopen(data/votes.txt.lock) [function.fopen]: failed to open stream: Permission denied in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 183
Warning: flock() expects parameter 1 to be resource, boolean given in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 184
Warning: fwrite(): supplied argument is not a valid stream resource in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 355
Warning: fclose(): supplied argument is not a valid stream resource in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 356
Warning: flock() expects parameter 1 to be resource, boolean given in /home/amemoryt/public_html/beta/test/sitepoint/poll/flatfile.php on line 196
Warning: file_get_contents(index.html) [function.file-get-contents]: failed to open stream: No such file or directory in /home/amemoryt/public_html/beta/test/sitepoint/poll/poll.php on line 127
Notice: Undefined variable: total_votes in /home/amemoryt/public_html/beta/test/sitepoint/poll/poll.php on line 135

I’ll see what I can do about this tonight.

Much appreciated by me and all the people who commented on the fact that net tuts posted it even though it’s broken in it’s unmodified form.

It looks to be a file permissions problem.

Here’s what the data files looked like beforehand.

-rw-r--r--  1 amemoryt amemoryt  2047 Feb  8 13:53 poll.js
-rw-r--r--  1 amemoryt amemoryt 55774 Feb  8 13:53 jquery.js

I know that my php engine runs as nobody. I would have to convert to suPHP to run with greater permissions.

So instead, I gave the data files full read/write access.


[~/www/beta/test/sitepoint/poll/data]# chmod 666 *

This resulted in the following permissions change to the data files

-rw-rw-rw-  1 amemoryt amemoryt    0 Feb  8 13:53 votes.txt
-rw-rw-rw-  1 amemoryt amemoryt    0 Feb  8 14:34 ips.txt

After running the page now, the data files update as per normal

-rw-rw-rw-  1 amemoryt amemoryt   11 Feb  8 16:40 votes.txt
-rw-rw-rw-  1 amemoryt amemoryt   15 Feb  8 16:40 ips.txt

So now that it works, does anyone know of a better solution regarding their permissions? Would setting them back to 644 and changing their user to nobody be a better alternative?