2

I need to retrieve some data from #__virtuemart_order_histories in templates/active_tmpl/html/com_virtuemart/orders/details_history.php via jQuery.ajax. I know that I can do it with a new &task= in controller but is it possible to make the ajax query to the DB in the above file without touching Virtuemart core files?

  • possible duplicate of joomla.stackexchange.com/questions/146/… – Sahil Purav Apr 23 '15 at 12:32
  • No, it's not a duplicate for your link contents the answer WITH changes in controller and other core files but I need a solution WITHOUT touching them – stckvrw Apr 23 '15 at 13:31
  • Please answer your own question and accept it whenever you are allowed to. – Sahil Purav Apr 23 '15 at 18:53
  • Do you have a custom component or plugin you could leverage with server side PHP? – Brian Bolli Apr 23 '15 at 21:26
1

I've found the following simple solution: to add in .../orders/details_history.php the script:

var response = jQuery.ajax({
  type: "GET",   
  url: "index.php?tmpl=raw&order_id=<?php echo $this->orderdetails['details']['BT']->virtuemart_order_id; ?>",   
  async: false
}).responseText;
alert(response);

and to create the file raw.php in the folder of active template with the query to the DB:

$order_id = $_REQUEST['order_id'];
$db = &JFactory::getDBO();
$query = $db->getQuery(true);
...

Any other better solution will be appreciated

  • Please check another answer for a similar solution but that would be better in terms of good practices and not making you template dependent. – Alexandr Oct 26 '17 at 16:04
1

You can create a plugin and use the com_ajax extension for it. Please never, ever use $_REQUEST to receive any data from request. Especially if you are going to use the data for database requests. Please use the JInput object for this.

  • Thanks, but I've found the simpler way: JRequest::getVar('order_id') – stckvrw Apr 24 '15 at 13:16
  • @stckvrw - JRequest is deprecated. Please use JInput – Lodder Apr 24 '15 at 18:19
1

You can also create a view, in my case named "ajax". All you need to do is create a directory 'ajax' in the view directory. Inside this dir put in the view.html.php containing the display class:

public function display($tpl = null)    {
        //Get post data
        $input = JFactory::getApplication()->input;

        //do whatever you want to to

        echo json_encode($returnData);
        exit;
    }
}

It is important to set the exit; command, which prevents getting template html inside your request.

Inside your jQuery script, call this view with post data like this:

var post_vars = new Array();
post_vars.push({name:'somevar', value:'somevalue'});

var data = jQuery.post('index.php?option=com_mycomponent&view=ajax', post_vars);
data.done(function(reply_data) {
  var response_json = jQuery.parseJSON(reply_data);
  //do whatever you want
});
  • Not the clearest and not the best solution. – Alexandr Oct 26 '17 at 15:54
1

IMHO, the clearest solution for this situation would be:

adding &format=json to your query, and point it to whatever view you would like

var response = jQuery.ajax({
  type: "GET",   
  url: "index.php?option=com_virtuemart&view=order&order_id=<?php echo $this->orderdetails['details']['BT']->virtuemart_order_id; ?>&format=json"
});

Then you need to add view.json.php at components/com_virtuemart/view/order/ and do whatever you need at display() function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.