ababba2 Posted December 26, 2015 Share Posted December 26, 2015 Hi guys. I have a component installed on my joomla site. As any component, this have it's own controller.php . In this controller.php there is this function function videohitCount_function($vid) { $db = JFactory::getDBO (); $query = $db->getQuery ( true ); /** Execute query to update hitcount */ $query->clear ()->update ( $db->quoteName ( '#__hdflv_upload' ) )->set ( $db->quoteName ( 'times_viewed' ) . ' = 1+times_viewed' )->where ( $db->quoteName ( 'id' ) . ' = ' . $db->quote ( $vid ) ); $db->setQuery ( $query ); $db->query (); } Now I need, in a custom php page inside the same component, make an Ajax call to this function. I tried with this, taken by an already existing answer on this forum <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function videohitCount_function { // using jquery ajax method for calling the php script $.ajax({ // set this to the url of your php script for calling the LSC function url: '../controller.php', // if the result of the ajax request is ok then this function is called success: function(response) { // the variable 'response' will contain the output from your php script // as an example we'll use a javascript alert to show the output of the response alert(response); } }); } </script> But doesn't work. What should i do for perform this ajax call? Quote Link to comment https://forums.phpfreaks.com/topic/300019-ajax-call-to-joomlas-controller/ Share on other sites More sharing options...
Muddy_Funster Posted January 4, 2016 Share Posted January 4, 2016 the php function is looking for a parameter called ($vid), you would need to catch and pass this info through the ajax call, and then tweak the php code in the controller to check for the post value in the event that the page is being loaded through an ajax call and then call the function using the post variable in the parameter. ajax: //make the content of your JS function something like this $.ajax({ method: "post", url:"...control.php", data: {'sender': 'customAjaxMethod', 'vid': $(this).attr("data-id")}, //<<-- this is just an example, you should change $(this).attr("data-id") to whatever holds the vidID reference in the DOM element. success: function(data){ ... } }) PHP: // add something akin to the following to the control.php file if(!isset($_POST['sender']){} elseif(!isset($_POST['vid'] || $_POST['sender'] != "customAjaxMethod"){} else{ videohitCount_function($_POST['vid']); } This obviously isn't meant as a copy and paste solution, only a guideline for you to work from. Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/300019-ajax-call-to-joomlas-controller/#findComment-1529072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.