How to INSERT dynamic selection into MySQL - Editor
How to INSERT dynamic selection into MySQL - Editor
Hey all,
I'm new here, and am having a little trouble with dynamic data. I can't seem to find any built in functions with Editor that will allow me to call a list of options from the database and have the Editor update my database with the selection.
I did come across this post: https://www.datatables.net/forums/discussion/9899/datatable-editor-form-select-ipopts-dynamic-values
Using the code suggested here (updated with mysqli) I now have the javascript pulling the values from the database. Great!
However when I click "New" enter all my info, select the option from the dropdown list (that was pulled dynamically) and click "Create", all of the info is INSERTed into the database record fine except... The dynamic selection.
Any help is appreciated!
Here's a snip of my code:
script within received.php:
<script type="text/javascript" language="javascript" class="init">
var test= new Array({"label" : "a", "value" : "a"});
function loader(){
test.splice(0,1);
$.ajax({
url: 'get_fields.php',
async: false,
dataType: 'json',
success: function (json) {
for(var a=0;a<json.length;a++){
obj= { "label" : json[a][0], "value" : json[a][1]};
test.push(obj);
}
}
});
return test;
}
</script>
Now this is called in my form object here:
...
fields: [{
label: "Date Received:",
name: "report.dateReceived",
type: "date"
}, {
label: "Brand:",
name: "brand.brandName",
type: "select",
"ipOpts":loader()
},
etc...
and this is my get fields.php
<?php
include "connect.php";
$sql="SELECT * FROM brand ORDER BY brandID ASC";
$result = mysqli_query($link,$sql);
$stack=array();
while($row = mysqli_fetch_array($result))
{
array_push($stack,array($row['brandName'],$row['brandID']));
}
echo json_encode($stack);
This question has an accepted answers - jump to answer
Answers
Okay so I opened a support ticket and got the answer I needed. When I finally looked closely at it, it solved my exact problem. So I'm sharing for any one else interested!
Thanks for getting in touch. The list of options in a
select
input can be populated automatically by the data that is loaded into the DataTable. There is an example of that available here:http://editor.datatables.net/examples/simple/join.html
Specifically, note that the
select
list contains a list of location options. If you click the "Ajax load" tab below the table and scroll to the bottom of the rendered data, you will see anoptions
object that defines those options.If you then click the "Server script" tab you will see an
options()
method being used for theuser.site
field which is what is providing that list of options.