Posted in Javascript, JQuery, JSON

Javascript – Populating Select 2 from JSON data

function getVersion (questionnaire) {

$(‘#_VERSIONS’).children(‘option:not(:first)’).remove();

$.post(‘inl_sev_042_J_nvg.php’,{“QUESTIONNAIRE” : questionnaire}, function(data) {

var i;

for (i = 0; i < data.length; i++) {

text = data[i];

newOption = new Option(text, i, false, false);

$(‘#_VERSIONS’).append(newOption).trigger(‘change’);

}

}, ‘json’);

}


 

// — Parameters input ——————————————————–

$questionnaire = (isset($_REQUEST[‘QUESTIONNAIRE’])) ? $_REQUEST[‘QUESTIONNAIRE’] : ”;

// — Define containers ——————————————————-

$terugvoer = array();
$tmp = array();

// — Retrive the present list of versions ———————————–

$sql = “SELECT DISTINCT _VERSION FROM questionnaire WHERE _TEMPLATE = ‘$questionnaire'”;

$db = new MysqliDb(‘CES’);

$selectStatus = $db->rawQuery($sql);

if ($selectStatus > 0) {

foreach ($selectStatus as $row) {

$terugvoer[] = $row[‘_VERSION’];

}

}

// echo ‘<pre>’;
// print_r($terugvoer);
// echo ‘</pre>’;

echo json_encode($terugvoer);

?>