Requesting a project calls AJAXTaskProjectsSelectRequest(project_id) ... that rebuilds the projects dropdown (id of 'viewTaskProject'). It's slightly messy to cascade through 3 functions, but it works within the application design.
Code: Select all
function AJAXTaskProjectsSelectRequest(project_id) {
var url = 'ajax/get_project_select.php?projectsIdSelected='+project_id;
var myAjax = new Ajax.Request(
url,
{
method: 'get',
onSuccess : AJAXTaskProjectsSelect,
onException : onExceptionRequest,
onFailure : onFailureRequest
});
}
function AJAXTaskProjectsSelect(xhr, json) {
var selected = json.projectsIdSelected;
var options = json.projectsArray;
rebuildSelect('viewTaskProject',selected,options);
}
function rebuildSelect(selectElementId, selectedId, optionsArray) {
var selectElement = document.getElementById(selectElementId);
while(selectElement.hasChildNodes() == true) {
selectElement.removeChild(selectElement.childNodes[0]);
}
if (optionsArray.length>0) {
for (var h=0; h < optionsArray.length; h++) {
newOption = document.createElement("option");
newOption.value = optionsArray[h].id;
if (optionsArray[h].id == selectedId) { newOption.selected = true; }
newOption.appendChild(document.createTextNode(optionsArray[h].title));
selectElement.appendChild(newOption);
}
}
}Code: Select all
<?php
include_once("../includes/constants.inc.php");
$sql = "select ";
$sql .= "* ";
$sql .= "from ";
$sql .= "int_project ";
$sql .= "where 1 ";
$sql .= "order by title asc ";
$result = mysql_query($sql,$databaseLink);
$jsonFields[] .= "\"totalProjects\" : ".mysql_num_rows($result);
$jsonFields[] .= "\"projectsIdSelected\" : ".$_GET['projectsIdSelected'];
if (mysql_num_rows($result) > 0) {
while ($record = mysql_fetch_object($result)) {
$projects[] = "{ \"id\" : \"".$record->project_id."\", \"title\" : \"".$record->title." (".$record->client_id.")\" }";
}
} else {
$projects[] = "{ \"title\" : \"No projects found\", \"id\" : \"\" }";
}
$jsonFields[] = "\"projectsArray\" : [".implode(", ",$projects)." ]";
$jsonText = "{ ".implode(", ",$jsonFields)." }";
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("X-JSON: $jsonText");
echo $jsonText;
?>