NicolasCARPi / jquery_jeditable

jQuery edit in place plugin. Extendable via plugin architecture. Plugins for plugin. Really.
https://jeditable.elabftw.net
MIT License
1.74k stars 458 forks source link

Using JSON to fill a dropdown selection #246

Closed kkaiser1952 closed 2 years ago

kkaiser1952 commented 2 years ago

I’d like the return from the MySQL in the <td below, the call is in the jQuery below that and PHP below that. But nothing ever shows up as a dropdown on the page. There should be 4 items, BIKES, CARS, PLANES, TRAINES. The field clears as it should but no dropdown appears, where did I go wrong?

        <td $timeline $brbCols $badCols 
            class=\"editable editfacility c33 \"    
            id=\"facility:$row[recordID]\"      
            onClick=\"empty('facility:$row[recordID]');\"> 
                <div class='$class'> $row[facility] </div>         
        </td>

    // with JSON feed
         $(".editfacility").editable("save.php", {
            type   : "select",
            loadurl : "GetListOfFacilities.php",
            submit : "OK",
            style  : "inherit"
         });

<?php
// This query gets and formats the selection criteria for the facility column 

require_once "dbConnectDtls.php";

    $q = $_POST['q'];
    //echo $q;
    $q = 'MYOWN';  

    $sql = "SELECT facility
              FROM `facility`
             WHERE groupcall = '$q'
           ";

            $listarray = array();
        foreach($db_found->query($sql) as $row) {
            $listarray[] = $row;
        } // end foreach

           //write to json file
                $fp = fopen('empdata.json', 'w');
                    fwrite($fp, json_encode($list));
                    fclose($fp);
?>

The output looks like this;

[{"facility":"CARS and Trucks","0":"CARS and Trucks"},{"facility":"PLANES including Private","0":"PLANES including Private"},{"facility":"TRAINS including Subways","0":"TRAINS including Subways"},{"facility":"BIKE And Motorcycles","0":"BIKE And Motorcycles"}]
NicolasCARPi commented 2 years ago

Hello,

This is what your json should look like:

{"D":"Letter D","E":"Letter E","F":"Letter F","G":"Letter G","selected":"selected one"}

(from https://jeditable.elabftw.net/json.php)

Here yours is in an array and doesn't have the same structure.

Please note that this library is deprecated and I'm currently focusing on https://github.com/deltablot/malle/.

kkaiser1952 commented 2 years ago

Two questions if I may. One: I thought I was using a prescribed method to change the output of my MySQL into JSON. I see now it didn't do the job. Should I just manually output the data in that format instead? Two: I use jQuery in so many places I my C.R.U.D. app would there be a reasonable reason to switch to Malle?

NicolasCARPi commented 2 years ago
  1. You should just take the first element of the array and it should encode fine in json.
  2. No reasonable reason for you to switch, no.
kkaiser1952 commented 2 years ago

Thanks for that info both 1 and 2. The output of MySQL now looks like this;

{"CARS and Trucks":"CARS and Trucks","PLANES including Private":"PLANES including Private","TRAINS including Subways":"TRAINS including Subways","BIKE And Motorcycles":"BIKE And Motorcycles",}

Which I do in php as; echo "$list"; but I'm missing the mechanism to move that data back to the for the dropdown. The example (thanks by the way) doesn't spell it out for me.

kkaiser1952 commented 2 years ago

I'm missing the mechanism to move that data back to the for the dropdown. The example (thanks by the way) doesn't spell it out for me.

kkaiser1952 commented 2 years ago

Never mind. I figured it out after many hours of trial and error I had a capital letter where I didn't want it. OH MAN!