Home Top Projects Tutorials Articles Submit Project
 
  • .NET Frameworks
  • Java Frameworks
  • PHP Frameworks
  • Ruby Frameworks
  • Other Frameworks
  • Cool AJAX sites
  • Ajax Resources
  • Ajax Tools
  • JavaScript frameworks
 
     
    • PHP and jQuery based 5-star Ajax Rating Script
    • A simple AJAX (JQuery) Contact Form with PHP Validation
    • Pseudo-custom events in Prototype 1.6
    • To ExtPHP, or to PHP-Ext?
    • PHP & Ajax help
    • Creating a simple AJAX website with example
    • Create a Cool Animated Navigation with CSS and jQuery
    • Bridging to Open Ajax
    • AJAX Chat Tutorial
    • Refresh Content in UpdatePanel using Asp.net Ajax Timer Control
    Home » Tutorials » Jquery vote

    Jquery vote

    Jquery vote helps you to display user votes on blog post. Here is the Database Design

    Messages Table :

    CREATE TABLE messages(
    mes_id INT PRIMARY KEY AUTO_INCREMENT,
    msg TEXT,
    up INT,
    down INT);

    Voting_IP Table : Storing IP address
    CREATE TABLE Voting_IP(
    ip_id INT PRIMARY KEY AUTO_INCREMENT,
    mes_id_fk INT,
    ip_add VARCHAR(40),
    FOREIGN KEY(mes_id_fk)
    REFERENCES messages(mes_id));

    Here is the class name of anchor tag. Using element.attr("id") calling vote button value(messsage Id):

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/
    libs/jquery/1.3.0/jquery.min.js"></script>
    <script type="text/javascript">
    $(function() {
    $(".vote").click(function()
    {
    var id = $(this).attr("id");
    var name = $(this).attr("name");
    var dataString = 'id='+ id ;
    var parent = $(this);

    if (name=='up')
    {
    $(this).fadeIn(200).html('<img src="dot.gif" />');
    $.ajax({
    type: "POST",
    url: "up_vote.php",
    data: dataString,
    cache: false,

    success: function(html)
    {
    parent.html(html);
    }
    });
    }
    else
    {
    $(this).fadeIn(200).html('<img src="dot.gif" />');
    $.ajax({
    type: "POST",
    url: "down_vote.php",
    data: dataString,
    cache: false,

    success: function(html)
    {
    parent.html(html);
    }
    });
    }
    return false;
    });
    });
    <script

    //HTML Code

    <?php
    include('config.php');
    $sql=mysql_query("SELECT * FROM messages LIMIT 9");
    while($row=mysql_fetch_array($sql))
    {
    $msg=$row['msg'];
    $mes_id=$row['mes_id'];
    $up=$row['up'];
    $down=$row['down'];
    ?>
    <div class="main">
    <div class="box1">
    <div class='up'>
    <a href="" class="vote" id="<?php echo $mes_id; ?>" name="up">
    <?php echo $up; ?></a></div>

    <div class='down'>
    <a href="" class="vote" id="<?php echo $mes_id; ?>;" name="down">
    <?php echo $down; ?></a></div>
    </div>

    <div class='box2' ><?php echo $msg; ?></div>
    </div>

    <?php } ?>



    Contains PHP code:

    <?php
    include("config.php");
    $ip=$_SERVER['REMOTE_ADDR'];

    if($_POST['id'])
    {
    $id=$_POST['id'];
    $id = mysql_escape_String($id);
    //Verify IP address in Voting_IP table
    $ip_sql=mysql_query("select ip_add from Voting_IP where mes_id_fk='$id' and ip_add='$ip'");
    $count=mysql_num_rows($ip_sql);

    if($count==0)
    {
    // Update Vote.
    $sql = "update Messages set up=up+1 where mes_id='$id'";
    mysql_query( $sql);
    // Insert IP address and Message Id in Voting_IP table.
    $sql_in = "insert into Voting_IP (mes_id_fk,ip_add) values ('$id','$ip')";
    mysql_query( $sql_in);
    echo "<script>alert('Thanks for the vote');</script>";
    }
    else
    {
    echo "<script>alert('You have already voted');</script>";
    }

    $result=mysql_query("select up from Messages where mes_id='$id'");
    $row=mysql_fetch_array($result);
    $up_value=$row['up'];
    echo $up_value;

    }
    ?>
    Copyrights Reserved AjaxProjects.com 2006-2013, Powered by Enozom - Mobile Development Company -Privacy Policy