user_ajax JSON Fabrik 3.0.x Explained

Guzman

Member
After a lot of research in the forums and tests, i acomplished a JSON call with Fabrik 3.0.x, i think there's not a clear reference of how to do this with Fabrik 3.0.x. So i made this example and demo:

First an example of a 'one value' request, this doesn't require JSON

/components/com_fabrik/js/form_X.js
'X' replace with formid
Code:
//Request example
//javascript function we call from certain event
function myNameJS(){
    var url = 'index.php?option=com_fabrik&format=raw&task=plugin.userAjax';
        //value idPerson will be taken from the form
        //this i know works with field and dates
        //idPerson = document.getElement('input[name=tbl_people___idPerson]').value;
        //this with dorpdowns
        idPerson = $('tbl_people___idPerson').get('value');

    //we call the mootools Request function and send the method (user_ajax.php function to request) and idPerson
    new Request({
        url: url,
            data: {
            method: 'myNamePHP','idPerson': idPerson
        },
        onComplete:function(response){
            if(response != '') {
                //if the response is not empty
                //we update other field with the response
                document.getElement('input[name=tbl_people___name]').value = response;
            }
        }
    }).send();
}
/components/com_fabrik/user_ajax.php
PHP:
function myNamePHP(){
    //get idPerson value
    $id = JRequest::getVar('idPerson', '');
    //SET THE CORRECT CONNECTION
    $db = FabrikWorker::getDbo(false, 2);
    //set the query
    $qry = "SELECT `name` FROM `tbl_people` WHERE `idPerson`='$id'";
    $db->setQuery($qry) or die (mysql_error());
    //get the result
    $name = $db->loadResult();
    //send the response
    echo $name;
}
And finally we trigger the request in a button
On mouseup:
Code:
myNameJS();
You can see demo here:
http://develop.sc54.info/index.php/user-ajax-fabrik-3-0-x
Click te 'Request' button.


Now a JSON request example:

/components/com_fabrik/js/form_X.js
'X' replace with formid
Code:
//Request JSON example
//javascript function we call from certain event
function myJsonJS(){
    var url = 'index.php?option=com_fabrik&format=raw&task=plugin.userAjax';
        //value idPerson will be taken from the form
        //this i know works with field and dates
        //idPerson = document.getElement('input[name=tbl_people___idPerson]').value;
        //this with dorpdowns
        idPerson = $('tbl_people___idPerson').get('value');

    //we call the mootools Request function and send the method (user_ajax.php function to request) and idPerson
    new Request({
        url: url,
            data: {
            method: 'myJsonPHP','idPerson': idPerson
        },
        onComplete:function(response){
            if(response != '') {
                //if the response is not empty
                //we decode the JSON
                json = JSON.decode(response);
                if (json['errors'] != '') {
                    //if theres some error registered it displays them via alert and avoids fields update
                    alert("Error: " + json['errors']);
                }
                else {
                    //if the request is sucessful, the fields are updated with the received data
                    document.getElement('input[name=tbl_people___lastname]').value = json['lastname'];
                    document.getElement('input[name=tbl_people___username]').value = json['username'];
                }
            }
        }
    }).send();
}
/components/com_fabrik/user_ajax.php
PHP:
function myJsonPHP(){
    //get idPerson value
    $idPerson = JRequest::getVar('idPerson', '');
    //declare the array for the response 
    $response = array();
    //set cleared errors
    $response['errors'] = '';
    //SET THE CORRECT CONNECTION
    $db = FabrikWorker::getDbo(false, 2);
    //set the query
    $qry = "SELECT `lastname`, `username` FROM `tbl_people` WHERE `idPerson`='$idPerson'";
    $db->setQuery($qry) or die (mysql_error());
    //get the result
    $data = $db->loadObjectList();
    if (!$data) {
        //report error
       $response['errors'] = 'No person with this idPerson: '.$idPerson;
        } else {
        //if the query gets a coincidence
        //complete the response
            foreach ($data AS $row) {
                $response['lastname'] = $row->lastname;
                $response['username'] = $row->username;
                }
        }
    //send the JSON encoded response
    echo json_encode($response);
And finally we trigger the request in a button
On mouseup:
Code:
myJsonJS();
You can see demo here:
http://develop.sc54.info/index.php/user-ajax-fabrik-3-0-x
Click te 'JSON' button.


That's it
DiGuz
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top