How to populate HTML dropdown with database values from json

163 views Asked by At

I've been looking everywhere but I can't seem to find a solution to my Problem. I have a table that is editable with JEditable, what I want to do is add a dropdown menu to it that gets its values from a json file in which I make a SQLI query to get the data. The Code for using the json file is from JEditable and works if I use an Array in plain text in the json. if I want to use the Query it breaks though. Any advice?

My Edit.js file has this:

   $(".editable-select-REG").editable("save.php", {
        type   : "select",
        loadurl : "REG.php",
        loadtext : "Fetching JSON…",
        submit : "OK",
        style  : "inherit"
    });

My HTML Dropdown is this: (just for clarification this works with the plaintext Array so no Need to Change this i guess)

    <td rowspan="2"><span class="editable-select-REG" name="REG"/><?php echo $row ['reg'] ?></td>

This is where the Problem in my json.php lies: This works:

    <? php
     $array['test1'] =  'test1';
     $array['test2'] =  'test2';
     $array['test3'] =  'test3';
     $array['test4'] =  'test4';
     $array['selected'] =  'selected one';
     echo json_encode($array);
     ?>

This is what i want basically:

 <?php
 include('config.php');

$sql = "SELECT reg FROM lfz";

$result = mysqli_query($db, $sql) or die("Error: ".mysqli_error($db));;

$regs = array();

while ($row = mysqli_fetch_array($result))
{
    array_push($regs, $row["reg"]);
}

echo json_encode($regs);
?>

As I said the basics work i just need help with the json.php file to get my values from the database.

1

There are 1 answers

1
Chris Pyro On

I just used this Code for the Array:

<?php
include('config.php');

$sql = "SELECT reg FROM lfz";

$result = mysqli_query($db, $sql) or die("Error: ".mysqli_error($db));;

$regs = array();
while ($row = mysqli_fetch_array($result))
{
    $regs[$row['reg']] = $row['reg'];
}
echo json_encode($regs);
?>

It did give me the correct Array from the beginning. the reason it didnt Show up in the Dropdown was because the old Array was somehow interfering. I deleted it and now it works.