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 PuravCommented Apr 23, 2015 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– stckvrwCommented Apr 23, 2015 at 13:31
-
Please answer your own question and accept it whenever you are allowed to.– Sahil PuravCommented Apr 23, 2015 at 18:53
-
Do you have a custom component or plugin you could leverage with server side PHP?– Brian BolliCommented Apr 23, 2015 at 21:26
4 Answers
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.– AlexandrCommented Oct 26, 2017 at 16:04
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')
– stckvrwCommented Apr 24, 2015 at 13:16 -
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
});
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.