Ajax Voting Script

from coursesweb.net

This is a Voting Script to vote Up /Down, or to simple add and display points /votes for images, articles, videos, games, or any element in pages of your web site.
This Voting Script is made using Ajax technology, with JavaScript and PHP (with OOP), it can be included in any page (".html", or ".php"), but the server must run PHP.
The votes for each element can be saved in TXT files on the server, or in a MySQL database. By default, the script saves data in TXT files.
The voted item, the number of the day, and the user (name or IP) are saved on the server, and cookies, so, the user can vote again the next day.
You can use the script to add rating system to one or more elements in a web page.
If you have a register /login script in your web site, you can set this Voting Script to allow only the logged users to vote. By default, all the visitors can vote an element once to 24 hours.

Installation and Settings

1. Copy on your server the directories: "votingfiles", and "votingtxt" (with all their files), in the same folder where you have the files in which you want to include the script.
2. In the <head> </head> zone of the page in which you want to use this script, add the following 2 lines of code (for CSS styles, and JavaScript functions):
<link href="votingfiles/voting.css" rel="stylesheet" type="text/css" />
<script src="votingfiles/voting.js" type="text/javascript"></script>
3. In the place /places of the web page where you want to display the voting system, add the following code:
  a) For the voting system with 2 buttons (Up and Down), that shows Total Votes, and Current Rating (uses: class="vot_updown1"), add:
<div class="vot_updown1" id="vt_??"></div>
  b) For the voting system with 2 buttons (Up and Down), that shows Current Rating, and the Number of Up and Down votes to each button (uses: class="vot_updown2"), add:
<div class="vot_updown2" id="vt_??"></div>
  c) For simple voting, with one button that adds one vote /point (uses: class="vot_plus"):
<div class="vot_plus" id="vt_??"></div>
- Replace the two "??" characters with the name /ID of the element you want to be voted. The voting button added by JavaScript in this DIV will be associated to that element. This name (or ID) must be unique, and always after the characters: "vt_" in the "id" attribute.

For example, if you want to apply the voting system with two buttons (Up /Down) to an image, "img_name.jpg", and the simple Vote (with one button) to an article, both in the same page, add under each one a DIV with the codes presented above The "id" attribute in each DIV will be: id="vt_img_name", and id="vt_article1". Similarly you can proceed with multiple elements.
• You can see how it is added in the file "test.html" (which can be used for test, if it is accessed from the server).

If the script is used in multiple pages of your website, you should be aware to not have two identical names, even if they are in different pages, because they are saved in the same ".txt" file (or mysql table).

4. The "votingtxt" directory, with its TXT files: "voting.txt", and "votusers.txt" are necesary only if you want to save the rating data on TXT files.
In this case you must set read-write-execute permissions to these files, CHMOD 0777 (or 0755) so the PHP can write data in these .txt files.

- If you want to save data in MySQL database, you can ignore /delete the "votingtxt/" directory (the PHP server must support PDO for connection to MySQL).
In this case, open the file "voting.php" (in the "votingfiles/" folder), and edit the following data:
    1) To set the script to use MySQL, change the value of the constant SVOTING from 'txt' to 'mysql' :
define('SVOTING', 'mysql');
    2) For connecting to your MySQL database:
define('DBHOST', 'localhost');            - replace localhost with your MySQL server address
define('DBUSER', 'root');                 - replace root with your database user name
define('DBPASS', 'passdb');               - replace passdb with your password for MySQL database
define('DBNAME', 'dbname');               - replace dbname with the name of your MySQL database
3) Create the tables in MySQL database. To create the tables, open in your browser the file "votingfiles/create_tables.php" .
(In your browser you must access an address like: "http://localhost/votingfiles/create_tables.php"), "localhost" is the domain name.

Other settings

- If you want the user to can vote only a single item in one day, change the value of the constant NRVOT from 0 to 1.
define('NRVOT', 0);

If your web site has a register /login script, and you want that only the logged users to can rate the elements, set the value of USRVOTE to 0 . Then set $_SESSION['username'] with the session your script uses to keep logged users; to the following code, in "voting.php"
define('USRVOTE', 1);
if(USRVOTE !== 1) {
  if(!isset($_SESSION)) session_start();
  if(isset($_SESSION['username'])) define('VOTER', $_SESSION['username']);
}


In this Voting Script, the user can vote again an item next day.
• If you want to set another time when the user can vote again, for example over 2 days, next week, or you want the user to can vote an item a single time only, use the PHP files from the "voting_up_down_set_time.zip" archive:
    - Replace the "class.voting.php", "create_tables.php", and "voting.php" with the files from this archive.
    - See the comments in "voting.php". Then access the "create_tables.php" to create the tables in MySQL database.

With these php files, data can be stored only in MySQL database.

- If you want to change the design, edit the "voting.css" file.
- To change the images for vote buttons, modify the files: votplus.gif, votup.png, votdown.png , keeping the same file name.

The script has been tested successfully on Mozilla Firefox, Internet Explorer, Google Chrome and Opera.
You can test it online on the page: Ajax Voting Script - Vote Up Down.

- The JavaScript instructions, and the names in CSS, are related, if you change something without knowing exactly what it does, it may not display / work properly.

With respect, CoursesWeb.net