Jump to content

Ajax call to joomla's controller.


ababba2

Recommended Posts

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?

 

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.