how to get database value from ajax in joomla modu

2020-04-10 03:11发布

I am creating a module where i want to get database value from ajax. anybody have a solution of this problem or any example please help me... can anyone give me a proper way for this.......

this is my jquery code-

jQuery('.type').bind('click', function() {
      var feedId = jQuery(this).attr('id');
      alert(feedId);
    jQuery.ajax({
        type: "POST",
        url: "modules/mod_feedback/ajax.php",
        data: {"Type":feedId},
        success: function(reviews){
            //jQuery(".show").html(response);
            alert(reviews);
        }
    });
  });

and this is my ajax.php code-

require_once( 'helper.php' ); 
$reviews = modfeedbackHelper::getFeedbackResultIdea();
echo $reviews;

2条回答
▲ chillily
2楼-- · 2020-04-10 03:43

Here is the ajax Call.

 $.ajax({
            type: "POST",
            url: "//PATH of ajax.php",
            data: { //Data to pass},
            cache: false,
                success: function(html)
                {
                    //DO SOMETHING  
                }
            });

and in ajax.php

 require_once( 'helper.php' );

 $reviews = modfeedbackHelper::getFeedbackResultIdea();

 echo $reviews; exit;

That should do :):)

查看更多
3楼-- · 2020-04-10 03:50

First of all you're trying to make an ajax call to the files inside module and its not a part of the module, so it act like external php file

modules/mod_feedback/ajax.php is wrong. You have to make the ajax call to the related component controller like below.

jQuery('.type').bind('click', function() {
      var feedId = jQuery(this).attr('id');
      alert(feedId);
    jQuery.ajax({
        type: "POST",
        url: "index.php?option=com_yourcomponent&task=yourcontroller.your_function",
        data: {"Type":feedId},
        success: function(reviews){
            //jQuery(".show").html(response);
            alert(reviews);
        }
    });
  });

Then inside your controller file a function named with your_function

and have the following codes inside your function

require_once( JPATH_SITE.'/modules/mod_feedback/helper.php' );

 $reviews = modfeedbackHelper::getFeedbackResultIdea();

 echo $reviews; exit;

Then it will work otherwise you have to load Joomla frame work to your ajax.php its a bad idea. try to follow correct methods.

Hope its helps..

查看更多
登录 后发表回答