I am getting exactly what I need from my PHP code and query; except I am having a really difficult time brining that data up to the front-end in order to populate an HTML dropdown.
Here is what I have on the PHP side; all working fine
$app->get('/dlteopt', function ($request, $response, $args) { $which = $_GET['id']; if ($which) { if ($which == 'table_1'){ $sql = "SELECT item1 FROM daya.blahblah"; } else if ($which == 'table_2'){ $sql = "SELECT item2 FROM daya.blahblah2"; } else if ($which == 'table_3'){ $sql = "SELECT item3 FROM daya.blahblah3"; } $stid = oci_parse($this->db, $sql); $list = array(); while ($list = oci_fetch_array($stid, OCI_ASSOC)) { $list[] = $list; var_dump($list); // this outputs the correct array I need, but cant bring it to front correctly into dropdown } if (!@oci_execute($stid)) { $error = oci_error($stid); throw new Exception($error['message']); } oci_execute($stid); }
Here is the jQuery; the response
console log is only the flag (which
) variable I am sending with the get
request which determines which table to query via a user scenerio. the array I need is ommitted…
let which = $(frm).attr("id"); $.get('dlteopt', {id: which }, function (response) { console.log(response); // this just consoles as the $which var no array $.each(response, function(index, value) { // started logic to append values in option; but no array or obj found/brought in to iterate through, can handle this part if can get array }); });
html drop down; just standard HTML select with a placeholder until populated:
<select name='agent' id='agent'><option>Loading...</option></select>
what am I doing wrong here? or missing/forgetting?
Update: I am stuck on an error on the front-end, below is what console.log(response)
outputs executed within my $.get
. The data within the error log is correct; I just can’t seem to access it properly… 2D array issue?
jquery.js:502 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in ...... array(2) { ["OSF_ID"]=> string(8) "FAI-FSDO" [0]=> array(1) { ["OSF_ID"]=> string(8) "FAI-FSDO" } } array(2) { ["OSF_ID"]=> string(8) "SAC-FSDO" [0]=> array(1) { ["OSF_ID"]=> string(8) "SAC-FSDO" } } ..... etc
Advertisement
Answer
Consider the following PHP.
public function process($which) { if(isset($which)){ if ($which == 'a_table1'){ $sql = "SELECT a_table1 FROM data.blah1"; } else if ($which == 'a_table2'){ $sql = "SELECT a_table2 FROM data.blah2"; } else if ($which == 'a_table3'){ $sql = "SELECT a_table3 FROM data.blah3"; } $stid = oci_parse($this->db, $sql); oci_execute($stid); if (!@oci_execute($stid)) { $error = oci_error($stid); throw new Exception($error['message']); } $myData = array(); while ($list = oci_fetch_array($stid, OCI_ASSOC)) { array_push($myData, $list); } header('Content-Type: application/json'); echo json_encode($myData); } else { header('Content-Type: application/json'); echo json_encode(array("error" => "WHICH not assigned")); } }
This should then send back JSON Data of the Array.
Example
[{ "A_ID":"OJC-FCT" },{ "A_ID":"DAL-ATCT" },{ "A_ID":"AFF-MIL-TWR" },{ "A_ID":"CNO-ATCT" },{ "A_ID":"GSN-FCT" },{ "A_ID":"CGI-NFCT" },{ "A_ID":"NDZ-MIL-TWR" },{ "A_ID":"FCS-MIL-TWR" },{ "A_ID":"LAL-FCT" },{ "A_ID":"LNK-ATCT" },{ "A_ID":"CHD-FCT" },{ "A_ID":"FLG-FCT" },{ "A_ID":"MCN-FCT" },{ "A_ID":"SKA-MIL-TWR" }];
You can then use this in your Each Loop to build the Options.
var which = $(frm).attr("id"); $.get('dlteopt', {id: which }, function (response) { console.log(response); $("#agent").html(""); $.each(response, function(index, value) { $("<option>").html(value['A_ID']).appendTo($("#agent")); }); });
You can also simplify your PHP Output so it’s just the result array of items.
$myData = array(); while ($list = oci_fetch_array($stid, OCI_ASSOC)) { array_push($myData, $list['A_ID']); }
Then you loop will also be simplified.
$("#agent").html(""); $.each(response, function(index, value) { $("<option>").html(value).appendTo($("#agent")); });