1. Fabrik 3.7 is now available. This version contains critical security updates, please update as soon as possible. Please remember to backup your site before updating, we also recommend testing your updates on development sites if possible.

Parse json array with comma in user_ajax.php

Discussion in 'Standard Support' started by startpoint, Apr 11, 2017.

  1. startpoint

    startpoint Active Member

    Level: Standard
    I create json array and send to user_ajax.php with this code in form_XX.js file:
    Code (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.
     
  2. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
    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
     
  3. startpoint

    startpoint Active Member

    Level: Standard
    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: Apr 12, 2017
  4. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
    Try $input->json->get(...). Which should automatically decode the input, so no need to explicitly json_decode() it.

    -- hugh
     
  5. startpoint

    startpoint Active Member

    Level: Standard
    I tried, but result is NULL.
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
    You did add the JSON.encode() in the JS, right?

    -- hugh
     
  7. startpoint

    startpoint Active Member

    Level: Standard
    Yes, i added decode function in my js file.
     
  8. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
    Encode or decode?

    -- hugh
     
  9. startpoint

    startpoint Active Member

    Level: Standard
    Sorry, encode. I made a mistake when writing post.
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
    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
     
  11. startpoint

    startpoint Active Member

    Level: Standard
    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?
     
  12. startpoint

    startpoint Active Member

    Level: Standard
    Friendly bump
     
  13. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
    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
     
  14. startpoint

    startpoint Active Member

    Level: Standard
    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
     
  15. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Supporter
  16. startpoint

    startpoint Active Member

    Level: Standard
    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.
     

Share This Page