3

I have seen a lot of inquiries about ajax, but normally there are about retrieving data from database. I was wondering about how to insert data directly interacting inside a module.

I have a module with listings retrieved from database and I would like that registered users can keep certain information from the module to be accessed later. It would be fine just to have a button near each element from the list to save it to a table maybe with two columns: User and Element ID.

I have researched and it seems that is easy using a wrapper calling a php script that inserts the records, but I am sure that this is a problem for security. Any ideas, please?

  • I'd recommend including some of the code that you're seen that is close. – David Fritsch Jan 13 '15 at 4:58
2

It is perfectly possible. Check this com_ajax example, saving information in the user session:

Using Joomla Ajax Interface https://docs.joomla.org/Using_Joomla_Ajax_Interface

Ajax-Session-Module https://github.com/Joomla-Ajax-Interface

Ajax-Session-Plugin https://github.com/Joomla-Ajax-Interface/Ajax-Session-Plugin

If you are going to implement your own tables, all requests must be associated with the current userid in module for "registered" users only.

2

I've done something similar recently except i'm deleting from the database. I'm guessing you can modify the code to update instead.

This is the ajax call in default.php

<script>
function ajaxDeleteEvent(finput)
{
    $.ajax({
        url:"index.php?   option=com_jobApplications&task=deleteApplication&id", 
        method:'POST',
        data: "id="+finput,
        success:
            function(data)
            {
                $("#container-"+finput).remove();
                console.log(data); 
            },
        error:
            function(e)
            {   
                console.log("u done goofed");
                console.log(e.message);
            }
    });
}
</script>

And in controller.php i have

public function deleteApplication()
{
    $mainframe = JFactory::getApplication(); 
    $id = JRequest::getVar("id",null,"post","int");
    // Check if id is set
    if ( isset( $id ) )
    {
        // remove that application from database
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        ...

    }   
    $mainframe->close();
}
1

You need to have a Controller method that can be called by ajax request (may this help).

This method will insert in the database using the db instance of joomla.

Then, all you need is is call the ajax by a onClick event in your module.

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.