Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax调用Joomla中的控制器?_Php_Jquery_Ajax_Joomla - Fatal编程技术网

Php Ajax调用Joomla中的控制器?

Php Ajax调用Joomla中的控制器?,php,jquery,ajax,joomla,Php,Jquery,Ajax,Joomla,好的,我为Joomla安装了一个组件,但现在我正在为添加自定义计数器视图而工作 在组件的controller.php中,我添加了以下函数: function updateView() { $db = JFactory::getDBO(); $videoid = JRequest::getVar('videoid'); if ($videoid) { $query = "update #__hdflv_upload SET times_viewed=

好的,我为Joomla安装了一个组件,但现在我正在为添加自定义计数器视图而工作

在组件的controller.php中,我添加了以下函数:

    function updateView() {
    $db = JFactory::getDBO();
    $videoid = JRequest::getVar('videoid');
    if ($videoid) {
        $query = "update #__hdflv_upload SET times_viewed=1+times_viewed where id=$videoid";
        $db->setQuery($query);
        $db->query();
    }
    $query = "select times_viewed from #__hdflv_upload where id=$videoid";
    $db->setQuery($query);
    $timeView = $db->loadResult();
    echo $timeView;
    jexit();
}
无论如何,在页面中,我尝试用php调用此函数,效果很好,但启用缓存后,脚本停止工作。他们对我说,在Joomla,反对意见应该总是在Ajax中。但我是Ajax方面的专家,我尝试过这个,但没有结果:

    <script>
$.ajax({
    url: 'index.php?option=com_contushdvideoshare&task=updateView&format=raw',
    success: function(data){
                if(!data.length){
            // Throw an error
            return;
        }

       $('.container').html(data);

    }
});
</script>

$.ajax({
url:'index.php?option=com\u contushdvideoshare&task=updateView&format=raw',
成功:功能(数据){
如果(!data.length){
//出错
返回;
}
$('.container').html(数据);
}
});

有人能帮我吗?谢谢

首先,您的第一个代码中存在sql注入漏洞,应该是:

function updateView() {
    $db = JFactory::getDBO();
    $videoid = JRequest::getInt('videoid');
    if ($videoid) {
        echo '0';
        jexit();
    }
    $query = "update #__hdflv_upload SET times_viewed=1+times_viewed where id=".(int)$videoid;
    $db->setQuery($query);
    $db->query();

    $query = "select times_viewed from #__hdflv_upload where id=".(int)$videoid;
    $db->setQuery($query);
    $timeView = $db->loadResult();
    echo $timeView;
    jexit();
}
使用JRequest::getInt('videoid')如果您想要检索一个整数,Joomla将为您检查和验证输入

您还可以在sql查询中直接转换为int,这是一个好习惯

然后,缓存get查询。要改变这种行为,您有两种解决方案:

  • Post请求从不缓存,因此请使用ajax Post请求
  • 在ajax函数中指定不应缓存此请求
下面是修改过的js代码

$.ajax({
    url: 'index.php?option=com_contushdvideoshare&task=updateView&format=raw',
    method: "POST", //Use post method
    cache: false, //Specify no cache
    success: function(data){
        if(!data.length){
            // Throw an error
           return;
        }
       $('.container').html(data);
    }