Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can i save the voting result and not revert when page refresh
    primarykey
    data
    text
    <p>Hi i have voting system,</p> <p>The problem is when i hit vote up/down its count the vote. But when i refresh/reload the page its back to default vote amount. Run the test on local machine only so far, with wamp.</p> <h2>This is the code from index.php</h2> <pre><code>&lt;?php include('config.php'); # connect mysql db dbConnect(); $query = mysql_query( 'SELECT id, first_name, last_name, film_info, vote FROM voting LIMIT 0 , 15'); ?&gt; &lt;!doctype html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="UTF-8" /&gt; &lt;title&gt;jQUery Voting System&lt;/title&gt; &lt;link href="http://netdna.bootstrapcdn.com/font-awesome/3.2.1/css/font-awesome.min.css" rel="stylesheet"&gt; &lt;link href="css/style.css" rel="stylesheet"&gt; &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script src="js/script.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="wrap"&gt; &lt;h1&gt;&lt;a href="#"&gt;Voting System&lt;/a&gt;&lt;/h1&gt; &lt;?php while($row = mysql_fetch_array($query)): ?&gt; &lt;div class="item" data-postid="&lt;?php echo $row['id'] ?&gt;" data-score="&lt;?php echo $row['vote'] ?&gt;"&gt; &lt;div class="vote-span"&gt;&lt;!-- voting--&gt; &lt;div class="vote" data-action="up" title="Vote up"&gt; &lt;i class="icon-chevron-up"&gt;&lt;/i&gt; &lt;/div&gt;&lt;!--vote up--&gt; &lt;div class="vote-score"&gt;&lt;?php echo $row['vote'] ?&gt;&lt;/div&gt; &lt;div class="vote" data-action="down" title="Vote down"&gt; &lt;i class="icon-chevron-down"&gt;&lt;/i&gt; &lt;/div&gt;&lt;!--vote down--&gt; &lt;/div&gt; &lt;div class="post"&gt;&lt;!-- post data --&gt; &lt;h2&gt;&lt;?php echo $row['first_name'].' '.$row['last_name']?&gt;&lt;/h2&gt; &lt;p&gt;&lt;?php echo $row['film_info'] ?&gt;&lt;/p&gt; &lt;/div&gt; &lt;/div&gt;&lt;!--item--&gt; &lt;?php endwhile?&gt; &lt;p style="text-align:center; margin-top: 20px"&gt;&amp;copy w3bees.com 2013&lt;/p&gt; &lt;/div&gt; &lt;?php dbConnect(false); ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <h2>This is the config.php</h2> <pre><code>&lt;?php # db configuration define('DB_HOST', 'locahost'); define('DB_USER', 'hendra'); define('DB_PASS', '123456'); define('DB_NAME', 'voter'); # db connect function dbConnect($close=true){ global $link; if (!$close) { mysql_close($link); return true; } $link = mysql_connect("localhost", "hendra", "123456") or die('Could not connect to MySQL DB ') . mysql_error(); if (!mysql_select_db("voter", $link)) return false; } ?&gt; </code></pre> <h2>This is the ajaxvote.php</h2> <pre><code>&lt;?php include('config.php'); # start new session session_start(); if ($_SERVER['HTTP_X_REQUESTED_WITH']) { if (isset($_POST['postid']) AND isset($_POST['action'])) { $postId = (int) mysql_real_escape_string($_POST['postid']); # check if already voted, if found voted then return if (isset($_SESSION['vote'][$postId])) return; # connect mysql db dbConnect(); # query into db table to know current voting score $query = mysql_query(" SELECT vote from voting WHERE id = '{$postId}' LIMIT 1" ); # increase or dicrease voting score if ($data = mysql_fetch_array($query)) { if ($_POST['action'] === 'up'){ $vote = ++$data['vote']; } else { $vote = --$data['vote']; } # update new voting score mysql_query(" UPDATE voting SET vote = '{$vote}' WHERE id = '{$postId}' "); # set session with post id as true $_SESSION['vote'][$postId] = true; # close db connection dbConnect(false); } } } ?&gt; </code></pre> <h2>and this is the jQuery code</h2> <pre><code>$(document).ready(function(){ // ajax setup $.ajaxSetup({ url: 'ajaxvote.php', type: 'POST', cache: 'false' }); // any voting button (up/down) clicked event $('.vote').click(function(){ var self = $(this); // cache $this var action = self.data('action'); // grab action data up/down var parent = self.parent().parent(); // grab grand parent .item var postid = parent.data('postid'); // grab post id from data-postid var score = parent.data('score'); // grab score form data-score // only works where is no disabled class if (!parent.hasClass('.disabled')) { // vote up action if (action == 'up') { // increase vote score and color to orange parent.find('.vote-score').html(++score).css({'color':'orange'}); // change vote up button color to orange self.css({'color':'orange'}); // send ajax request with post id &amp; action $.ajax({data: {'postid' : postid, 'action' : 'up'}}); } // voting down action else if (action == 'down'){ // decrease vote score and color to red parent.find('.vote-score').html(--score).css({'color':'red'}); // change vote up button color to red self.css({'color':'red'}); // send ajax request $.ajax({data: {'postid' : postid, 'action' : 'down'}}); }; // add disabled class with .item parent.addClass('.disabled'); }; }); }); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload