dongyou8368 2016-04-13 15:45
浏览 43

joomla通过ajax执行php函数

I'm using Joomla 2.5 and I want to execute a php function via ajax that do an update in an external database. This is my ajax call:

$.ajax({

        url: "index.php?option=com_prova&format=raw&task=updateReserve",
        data: { robotId: idRobot, reserved: book}
          }).done(function(response) {
              console.log(response);
    }); 

In components/com_prova I have a controller.php file that contains:

 class ProvaController extends JController
 {
/**
 * Method to display a view.
 *
 * @param   boolean  $cachable   If true, the view output will be cached
 * @param   mixed    $urlparams  An array of safe url parameters and their variable types, for valid values see {@link JFilterInput::clean()}.
 *
 * @return   JController        This object to support chaining.
 *
 * @since    1.5
 */
public function display($cachable = false, $urlparams = false)
{



    parent::display($cachable, $urlparams);

    return $this;
}



public function updateReserve(){



       $input = JFactory::getApplication()->input;
       $booked  = $input->get('reserved', '', 'INT');
       $robotId = $input->get('robotId', '', 'INT');
       $db = $this->external_db();
       $query = $db->getQuery(true);
       $fields = array(
        $db->quoteName('booked') . ' = ' . (int)$booked
        );
      $conditions = array(
        $db->quoteName('id') . ' = ' . (int)$robotId);
       $query->update($db->quoteName('robots'))->set($fields)->where($conditions);
       $db->setQuery($query);
       $result = $db->execute();



       }

        private function external_db()
        {
            $option = array();

            $option['driver']   = 'mysql';
            $option['host']     = 'xxxxxxxxxxx';
            $option['user']     = 'xxxxxxxxxx';
            $option['password'] = 'xxxxxxxxxx';
            $option['database'] = 'xxxxxxxxx';
            $option['prefix']   = '';

            $db = JDatabaseDriver::getInstance($option);

            return $db;
        }

}

But all of this does not work. I have this error on console:

  jquery-1.12.3.min.js:4 GET http://xxxxxx/xxxxxxx/index.php?option=com_prova&format=raw&task=updateReserve&robotId=2&reserved=1&lang=it 500 (Internal Server Error)

Why?What is the problem? Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计