从 db 动态请求一个项目,并在页面 joomla 上 post 它

dynamically request an item from db and post it on the page joomla

我正在努力实现以下目标:

  1. 页面加载时
  2. 从本地存储中获取数据(文章 id(s))
  3. 然后进行数据库查询以检索具有 id
  4. 的文章的文本
  5. 在同一页显示文章正文

我有这个代码

jQuery 代码(在 index.php 文件中 - 当用户在特定页面上时,此代码被激活)

 <script>
 //get the object from local storage and then get the id's of the articles 

 var plantList = JSON.parse( localStorage.getItem( 'myPlantList' ));

 var articleIds = [];
 for(i=0; i < plantList.length; i++){
 articleIds.push(plantList[i].itemId);
 console.log(articleIds +" array of the items"); //logs fine - this part works
 }

 //send the array of the id's via post

  jQuery.ajax({
    type: 'post',
    url: 'article_list.php',
    data:   {articleIds: articleIds}, 
    success: function(data){
        //jQuery(".art-layout-cell.art-content").append(data);
        console.log(data);//console logs an empty line ????
        console.log('success'); // console logs success
    },
    error: function(){
    console.log("error");
    }

    });

   </script>

article_list.php

  <?php

 defined('_JEXEC') or die;

 JHtml::_('behavior.framework', true);

 if ( isset($_POST["articleIds"]) ) {
 $articleIds = json_decode($_POST["articleIds"]);
 //$articleIds = array(28,30); // I have tried just to set the array, to see what will happen - this didn't work either

 $customArticleList ="<div id='mylist-articles'>";

 foreach($articleIds as $value) {

    $db = JFactory::getDbo();

    $query = $db->getQuery(true);
    $query->select($db->quoteName('introtext'));
    $query->from($db->quoteName('#__content'));
    $query->where($db->quoteName('id')." = ".$db->quote(intval($value)));

    $db->setQuery($query);

  // Load the results 
  $result = $db->loadResult();

  $customArticleList .= "<div class='list-article'>". $result. '</div>';

}

$customArticleList .= "</div>";

echo $customArticleList;

}else{
echo "nothing received";
}
?>

于是请求发出,数据接收回来。但是为什么我得到一个空数据结果????

调用数据库在 index.php 中工作正常(我刚刚将 $_POST 数组替换为预定义数组,如 array(1,2) - 并且调用数据库返回了我所需的文章...)

任何人请提示!

提前致谢!

我想我想自己回答我的问题..

口渴的事情是javascript:我必须声明预期的类型或响应,在我的例子中是html所以

     dataType: "html",

其次,问题还在于查看自定义 php 文件的 joomla 规则 而不是前两行

 defined('_JEXEC') or die;
 JHtml::_('behavior.framework', true);

我不得不补充:

 define( '_JEXEC', 1 );
 define('JPATH_BASE', '/../../../../public_html'); //path to the root folder of the joomla instalation
 require_once ( JPATH_BASE .'/includes/defines.php' );
 require_once ( JPATH_BASE .'/includes/framework.php' );

我希望这能在将来节省一些人的时间...