Simple voting concept based on ip address using php with Ajax | 2my4edge

31 December 2014

Simple voting concept based on ip address using php with Ajax

Simple voting concept based on ip address using jquery ajax with php mysql, most of the web developers looking for this concept. so today i'm going to create this in php and mysql with Ajax, without refreshing the page voting concept. so let see the coding of it.

simple voting style using php mysql ajax jquery
DOWNLOAD                    LIVE DEMO

FILES
  • db.php
  • index.php
  • vote.php
These files are needed to create simple ip based voting system. to find ip address 

DB.PHP
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', '2my4edge');
$connection = mysql_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD) or die(mysql_error());
$database = mysql_select_db(DB_DATABASE) or die(mysql_error());
?>

DATABASE DETAILS
Database name-->2my4edge

TABLES
post --> post_id(primary key auto increment),post_name(varchar),post_image(varchar),vote(int)
post_ip --> ip_id(primary key auto increment),post_id_fk(foreign key),ip_address(varchar)

create database and table as shown above in mysql.

INDEX.PHP
<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function()
{
  $("span.like_img").mouseover(function ()
  {
    $(this).addClass("liked_img");
  });

  $("span.like_img").mouseout(function ()
  {
    $(this).removeClass("liked_img");
  });
});

$(function() {
$(".like").click(function() 
{
var id = $(this).attr("id");
var dataString = 'id='+ id ;
var parent = $(this);
$(this).fadeOut(300);
$.ajax({
type: "POST",
url: "vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
parent.fadeIn(200);
} 
});
return false;
 });
});
</script>


<?php
include('db.php');
$fetch=mysql_query("SELECT * FROM post");
while($row=mysql_fetch_array($fetch))
{
$post_id=$row['post_id'];
$post_name=$row['post_name'];
$post_image=$row['post_image'];
$vote=$row['vote'];
?>
    
    <div class="container">
    <div class="title">
    <h3 align="left"> <?php echo $post_name; ?> 
    <span class="right"> <a href="#" class="like" id="<?php echo $post_id; ?>">
          <span class="like_img" align="left">
          <?php echo $vote; ?>
          </span>
        </a> </span>  </h3>
    </div> 
    <img src="<?php echo $post_image; ?>" >
    </div>    
    <?php
}
?>
the above is the index file, this page only visible to user. AJAX voting operation will be in vote.php file.

VOTE.PHP
<?php
include("db.php");
$ip=$_SERVER['REMOTE_ADDR']; 
if($_POST['id'])
{
$id=$_POST['id'];
$ip_fetch=mysql_query("SELECT ip_address FROM post_ip WHERE post_id_fk='$id' AND ip_address='$ip'");
$count=mysql_num_rows($ip_fetch);
if($count==0)
{
$fetch="UPDATE post SET vote=vote+1 WHERE post_id='$id'";
mysql_query($fetch);
$insert= "INSERT INTO post_ip(ip_address,post_id_fk) values ('$ip','$id')";
mysql_query($insert);
$result=mysql_query("SELECT vote FROM post WHERE post_id='$id'");
$row=mysql_fetch_array($result);
$vote=$row['vote'];
?>
<span class="like_img" align="left"><?php echo $vote; ?></span>
<?php
}
else
{
echo 'You already voted';
}
}
?>
functionality page, here only the voting process works by updating the value when we are clicking the class="like". here we are getting ip address, if ip address is comes more than a time it wont take vote, it allow only one vote for a ip address.

RELATED POSTS :

6 comments:

  1. thank u sir its very useful for me

    ReplyDelete
  2. i want to try i hope it's work

    ReplyDelete
  3. Since 2008 some states have adopted these new rules concerning state issued identification cards on the basis that it will prevent voter fraud. https://victorytips.sitey.me/

    ReplyDelete
  4. Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. https://192-168-i-i.com

    ReplyDelete
  5. What is an outstanding post! “I’ll be back” (to read more of your content). Thanks for the nudge! https://192-168-i-i.com/

    ReplyDelete
  6. Grandstream PBX System- Grandstream Distributor Cameroon VDS the Grandstream distributor in  Cameroon continues to bring innovative Grandstream Products to the IP communications / Telephony market with compelling values and features. Grandstream Networks is headquartered in Brookline, Massachusetts with offices in Yealink IP Phones

    ReplyDelete

^