• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CJAX Vote System Problem with multiple clicks

#1
[eluser]bubx[/eluser]
Hi,
I wrote a simple voting system, and I have a problem with multiple clicks. When I click Like ("Mocne"), I can then click on Unlike ("Słabe"), and a lot of times. I would like to be able to click only once, in one or the other. I use CJAX flush method, but something here does not work properly.

Article view:
Code:
<?php
$this->load->helper('phpHelper'); // plMiesiac

  require_once 'ajax.php';

  $ajax = ajax();
  
  $actionsPlus = array (
  $ajax->call('vote/plus/'.$id,'sum'.$id.'p')
  );
  $actionsMinus = array (
  $ajax->call('vote/minus/'.$id,'sum'.$id.'p')
  );
  
  $ajax->click("#plus".$id,$actionsPlus);
  $ajax->click("#minus".$id,$actionsMinus);

?>
<div class="bs-component">
              <div class="panel panel-default">
                <div class="panel-heading">
     <div >  &lt;?php echo '#'.$id; ?&gt;    </div>
     <div >&lt;?php echo 'Autor: '.$autor.' Dodany: ' ?&gt;
     &lt;?php echo date("d ",$date); ?&gt;
     &lt;?php echo plMiesiac(date("F",$date)); ?&gt;
     &lt;?php echo date("Y",$date); ?&gt;
     &lt;?php echo date("H:m",$date); ?&gt;
     </div>
     </div>
                <div class="panel-body">
                  &lt;?php echo $text ?&gt;
                </div>
    <div class="panel-footer">
    <div class="row">
     <div class="col-md-4">
      <a id="plus&lt;?php echo $id; ?&gt;" href="#"><span class="label label-success">Mocne: <span id="plus&lt;?php echo $id; ?&gt;p">&lt;?php echo $voteplus; ?&gt;</span></span></a>
      <a id="minus&lt;?php echo $id; ?&gt;" href="#"><span class="label label-warning">Słabe: <span id="minus&lt;?php echo $id; ?&gt;p">&lt;?php echo $voteminus; ?&gt;</span></span></a>
      <span class="label label-info">Punkty: <span id="sum&lt;?php echo $id; ?&gt;p">&lt;?php echo ($voteplus - $voteminus); ?&gt;</span></span>
      <span id="info&lt;?php echo $id; ?&gt;" class="label label-warning" ><span>
     </div>
     <div class="col-md-4">
      
     </div>
     <div class="col-md-4">
      <span class="label label-primary">Komentarze:</span>
      <span class="label label-primary">0</span>
     </div>
    </div>
    </div>
              </div>
</div>

Controller:
Code:
&lt;?php

class vote extends CI_Controller{

public function __construct() {
        parent:: __construct();

}

function plus($id){
         $this->load->model('Articles');

  require_once(FCPATH.'ajaxfw.php');
  $ajax = ajax();
  
   $ajax->flush('#plus'.$id);
   $ajax->click("#plus".$id,$ajax->call('vote/showInfo/'.$id,'info'.$id));
   $ajax->flush('#minus'.$id);
   $ajax->click("#minus".$id,$ajax->call('vote/showInfo/'.$id,'info'.$id));
  
  
  $query = $this->db->get_where('articles', array('id' => $id));
  foreach($query->result() as $row) {
   $teraz = $row->voteplus;
   $this->db->where('id', $id);
   $this->db->update('articles', array('voteplus' => $teraz+1));
   echo ($teraz+1)-$row->voteminus;
   }
}
function minus($id){
  $this->load->model('Articles');
  require_once(FCPATH.'ajaxfw.php');
  $ajax = ajax();

   $ajax->flush('#minus'.$id);
   $ajax->click("#minus".$id,$ajax->call('vote/showInfo/'.$id,'info'.$id));
   $ajax->flush('#plus'.$id);
   $ajax->click("#plus".$id,$ajax->call('vote/showInfo/'.$id,'info'.$id));
  
  $query = $this->db->get_where('articles', array('id' => $id));
  foreach($query->result() as $row) {
   $teraz = $row->voteminus;
   $this->db->where('id', $id);
   $this->db->update('articles', array('voteminus' => $teraz+1));
   echo $row->voteplus-($teraz+1);
   }
}
function showInfo($ids){
  require_once(FCPATH.'ajaxfw.php');
  $ajax = ajax();
  $ajax->flush('#plus'.$ids);
  $ajax->flush('#minus'.$ids);
  
  $ajax->replace('#info'.$ids,'<span id="info'.$ids.'" class="label label-warning" >Oddałeś już głos na ten wpis<span>');
}


}

#2
[eluser]CroNiX[/eluser]
I don't know anything about cjax, but you can always disable or remove a link/button via javascript with a click event.

#3
[eluser]bubx[/eluser]
I did so at the beginning, but for me it was not satisfying. I did this using the session.

Code:
$data = $this->session->userdata($id);
if($data==false){
  [...]
  $this->session->set_userdata($id,true);
  }else{
   [...]
  }

#4
[eluser]CroNiX[/eluser]
I'm not sure what you're wanting then. In you original post you said "I would like to be able to click only once, in one or the other"

#5
[eluser]bubx[/eluser]
I want that after use "Vote Up" or "Vote Down" the article was excluded from voting for that user (session).

#6
[eluser]CroNiX[/eluser]
It might be because you are using sessions (which uses a cookie) while using ajax. There are a number of problems with ajax and sessions in CI. Does it prevent voting if you navigate to a different page on the site after voting and then come back to it and try to vote again?

It might work better if you set a flag in the db that the user voted for that already and then check that when the data is submitted before accepting the vote.

Sorry, again I don't know anything about cjax. If it were jquery you could just use $(selector).off('click') to remove the click event.

#7
[eluser]bubx[/eluser]
Conceptually, this also must work for unregistered users, only for one session, and it works. For registered users i want to use database. I do not know too well cjax, but at this time it works fine.
Anyway, if there are problems with AJAX in CI, i lose tempted to use this framework Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.