• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

Parse json array with comma in user_ajax.php

startpoint

Active Member
I create json array and send to user_ajax.php with this code in form_XX.js file:
JavaScript:
var rows_array_data = new Array();

jQuery(".repeatGroupTable .fabrikSubGroup").each(function() {
    rows_array_data.push(
        data_id
    );
}

var data_id = rows_array_data;
var url = "index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=my_function&data_id=" + data_id;
new Request({url:url,
    dataType:'json',
    onComplete:function(r){
        var result = jQuery.parseJSON(r);
    }
}).send();

This is my function in user_ajax.php with needed query:
PHP:
public function my_function()
{
    $db = JFactory::getDbo();
    $app = JFactory::getApplication();
    $input = $app->input;
    $data_id = $input->get('data_id', '');
    $data_id2 = json_decode($data_id, true);

    $query = $db->getQuery(true);
    $query = 'Select `column1` from `table_name` where `id` IN (' . $data_id2 . ')';
    $db->setQuery($query);
    $result = $db->loadResult();
}

In this way all $data_id2 are merged.
How to change my code that value in $data_id2 to be separated with comma to get the query?
I tried many variants, but no luck.
 
The easiest way would probably be to encode the array when sending the request ... JSON.encode(data_id) ... then explode it after decoding it in the PHP ... $data_id2 = explode(',', $data_id2).

And you need to use something other than loadResult to load multiple rows, like loadColumn() ... and encode the $result and echo it to get it back to the calling JS .... echo json_encode($result)

-- hugh
 
I changed my code to:
PHP:
$data_id = $input->get('data_id', '');
$data_id2 = json_decode($data_id, true);
$data_id2 = explode(',', $data_id2);
but result is array with 1 key 0 and value all merged id's.
This is a from Params tab from console:
data_id ["532","533","534","535","536","537","538","539","540","541","542"]
format raw
method my_function
option com_fabrik
task plugin.userAjax
and in Response tab:
PHP:
<pre>array (
  0 => '5.3253353453554E+32',
)</pre>
This is very strange.
I thing the problem is in get('data_id') function.
Tried with names data (words with spaces). The result is the same merged names with missing parts which are not english words.
 
Last edited:
Try $input->json->get(...). Which should automatically decode the input, so no need to explicitly json_decode() it.

-- hugh
 
I really can't go much further on this. If you point me at the page I'll take a look at the Javascript. But there's a finite limit on how much support I can give for custom coding in Standard support.

Looking at the JS you quoted, I'm not sure that's going to work. I think you'll just create an array of DOM objects, not ids.

-- hugh
 
Data_id's are values who i take with jQuery('#element....').val() from repeated group. Then i create array and encode with json.encode(). That's not right?
 
I've pretty much exhausted my suggestions, I'd have to log in and write the code for you to fix it.

Sent from my HTC6545LVW using Tapatalk
 
The complete solution:
PHP:
public function my_function()
{
    $db = JFactory::getDbo();
    $app = JFactory::getApplication();
    $input = $app->input;
    $data_id = $input->getString('data_id');
    $data_id2 = json_decode($data_id);
    $data_id2 =  implode(", ", $data_id2);

    $query = $db->getQuery(true);
    $query = 'Select `column1` from `table_name` where `id` IN (' . $data_id2 . ')';
    $db->setQuery($query);
    $result = $db->loadAssocList();
    echo json_encode($result);
}
This is based on this documentation: https://docs.joomla.org/Retrieving_request_data_using_JInput
 
I tried with your suggestion, but with no luck.
With $input->getString() work properly.
I don't know is this best and right way, but work in my scenario.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top