title: How to create a simple vote by ajax in CakePHP 2.0
body: <p>I am just trying to create a simple vote with CakePHP 2.0 and Ajax. I am a newbie in this framework so i find it really difficult...</p>
  
  <p>I just want to create a link with a vote action which will call an action in a controller to update the field "numberVotes" in a table of the Database.</p>
  
  <p>I am trying it but i dont know if i am doing it well.
  I have this now:</p>
  
  <p><b>//posts/view.ctp</b> 
      $this->Html->script('votar', array('inline' => false)); //it loads it on the layout</p>
  
  <pre><code>echo '&lt;div id=\'vote\'&gt;';
      echo $this-&gt;element('vote', array('id' =&gt; $post['Post']['id']));
  echo '&lt;/div&gt;'
  </code></pre>
  
  <p><b>Elements/vote.ctp</b></p>
  
  <pre><code>if(!empty($voting)){
  echo "You have voted!!!";   
  }else{
  echo '&lt;a href="#" onclick="votar(\''.$id.'\');return false;"&gt;Vote here!!&lt;/a&gt;
  }
  </code></pre>
  
  <p><b>webroot/js/vote.js</b></p>
  
  <pre><code>//XMLHttpRequest  Ajax
  function newAjax()
  { 
  var xmlhttp=false; 
  try 
  { 
      xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
  }
  catch(e)
  { 
      try
      { 
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      catch(E) { xmlhttp=false; }
  }
  if (!xmlhttp &amp;&amp; typeof XMLHttpRequest!='undefined') { xmlhttp=new XMLHttpRequest();     } 
  return xmlhttp; 
  }
  
  
  function voting(num) {
  var url;
  var obCon = document.getElementById('vote');
  var ajax = newAjax();
  
  url = 'http://localhost:8888/mysite/posts/voting/' + num;
  alert(url);
  ajax.open("GET", url);  
  
  ajax.onreadystatechange=function(){
      if(ajax.readyState==4){
          if(ajax.status==200){
              obCon.innerHTML=ajax.responseText;
  
          }else if(ajax.status==404){
              obCon.innerHTML = "Page not found";
          }else{
              obCon.innerHTML = "Error:"+ajax.status; 
          }
      }
  }
  ajax.send(null);
  </code></pre>
  
  <p>}</p>
  
  <p><b>//Controllers/PostsController.php</b></p>
  
  <pre><code>public function voting($id = null){
              ... //stuff to add the vote in the DB
      $this-&gt;set(array('id' =&gt; $id, 'voting' =&gt; 'yes'));
      $this-&gt;render('/Elements/vote', false);
  }
  </code></pre>
  
  <p>I am sure i am not using the power of CakePHP for ajax... but i dont know where i can apply it or how to do it. 
  Any suggestion?</p>
  
  <p>Thanks.</p>