Je suis un débutant en programmation. En réalisant mon premier composant d'un ajax vote, je n'arrive pas d'appeler la fonction Vote dans le controller, ainsi dans le model. J'y suis bloqué plusieurs jours. Pouvez-vous m'indiquer où est mes erreurs, merci d'avance.
default.php
controller.php
models/Vote.php
view.html.php
default.php
Code HTML:
<?php defined( '_JEXEC' ) or die( 'Restricted access' ); ?> <script type="text/javascript"> function getXHR() { var xhr = null; if (window.XMLHttpRequest) { xhr = new XMLHttpRequest(); } else if (window.createRequest) { xhr = window.createRequest(); } else if (window.ActiveXObject) { try { xhr = new ActiveXObject('Msxml2.XMLHTTP'); } catch (e) { try { xhr = new ActiveXObject('Microsoft.XMLHTTP'); } catch (e) {} } } return xhr; } function ajaxVote(cid, votetype){ cid = parseInt(cid); votetype = parseInt(votetype); if ( cid > 0 && votetype >= 0) { var xhr = getXHR(); var url = "index.php?option=com_article&controller=vote"; document.getElementById('loadimg_'+cid).style.display = 'inline'; document.getElementById('voteButtons_'+cid).style.display = 'none'; xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200) { document.getElementById('loadimg_'+cid).style.display = 'none'; document.getElementById('pluscnt_'+cid).innerHTML = response.plus; document.getElementById('minuscnt_'+cid).innerHTML = response.minus; document.getElementById('voteButtons_'+cid).style.display = 'inline'; } } xhr.open("POST",url+"?cid"+cid+"&votetype"+votetype,true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(null); } } </script> <fieldset class="adminform"><legend><?php echo JText::_( 'Click For Vote' ); ?></legend> <?php echo JText::_('LIKE') ?> <span id='pluscnt_<?=$this->article->id?>'><?=$this->reponseplus?></span> <br/> <div id='voteButtons_<?=$this->article->id?>'> <input type='button' value='<?php echo JText::_('Like') ?>' style='width:120px' onclick='ajaxVote(<?=$this->article->id?>, 0)'><br/> <input type='button' value='<?php echo JText::_('NoLike') ?>' style='width:120px' onclick='ajaxVote(<?=$this->article->id?>, 1)'><br/> </div> <div id="loadimg_<?=$this->article->id?>" style="display: none;"></div> <?php echo JText::_('NOLIKE') ?> <span id='minuscnt_<?=$this->article->id?>'><?=$this->reponseminus?></span> <br/> </fieldset>
Code PHP:
function display(){
$view = JRequest::getVar('view');
if (!$view) {
JRequest::setVar('view', 'list');
}
parent::display();
}
function vote() {
// Check for request forgeries
JRequest::checkToken() or jexit('Invalid Token');
$mainframe = JFactory::getApplication();
$article_id = JRequest::getInt('cid', 0);
$votetype = JRequest::getInt('votetype', 0);
require_once(JPATH_COMPONENT.DS.'models'.DS.'vote.php');
$model = new articleModelVote();
$this->setRedirect(JRoute::_('index.php?option=com_article&view=article&id='. $article_id));
}
Code PHP:
function vote($article_id, $votetype) {
$mainframe = JFactory::getApplication();
$db = JFactory::getDBO();
$user =& JFactory::getUser();
$article_id = JRequest::getInt('cid','');
$votetype = JRequest::getInt('votetype', 0);
$query = "INSERT INTO #__article_vote
(article_id, user, votetype) VALUES ('{$article_id}', '{$user}', '{$votetype}')";
//Save the vote to the database
$db->setQuery($query);
if (!$db->query()) {
$msg = $db->stderr();
$tom = "error";
}
return true;
}
function getReponse() {
$db = JFactory::getDBO();
$article_id = JRequest::getInt('cid', 0);
$reponse = array();
$db->setQuery("SELECT COUNT(*) as votescount, article_id FROM #__article_vote WHERE votetype = 0 GROUP BY article_id");
$reponse['plus'] = $db->loadAssocList('article_id');
$db->setQuery("SELECT COUNT(*) as votescount, article_id FROM #__article_vote WHERE votetype = 1 GROUP BY article_id");
$reponse['minus'] = $db->loadAssocList('article_id');
return $reponse;
}
}
Code PHP:
function display($tpl = null){
$mainframe = JFactory::getApplication();
$id = (int) JRequest::getVar('id', 0);
$article =& JTable::getInstance('article', 'Table');
$article->load($id);
//Get total votes
$reponse = $this->get('Reponse');
$reponseplus = $reponse['plus'];
$reponseminus = $reponse['minus'];
$this->assignRef('article', $article);
$this->assignRef('reponseplus', $reponseplus);
$this->assignRef('reponseminus', $reponseminus);
parent::display($tpl);
}
Commentaire