Hallo ik ben nog niet zo heel lang bezig met PHP. Nu heb ik 2 bestanden waarmee ik via ajax medewerkers kan toevoegen. Alleen het werkt niet ik krijg namelijk de melding: van Firefox fout console:

Fout: responseXML has no properties
Bronbestand: http://localhost/test/employeeList.html
Regel: 87

updateEmployeeList() employeeList.html (line 87)
handleAddStateChange()

var status = responseXML.getElementsByTagName("status").item(0).firstChild.nodeValue;

Dus ik denk dat het iets met die responseXML te maken heeft?


employeeList.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">;
<html xmlns="http://www.w3.org/1999/xhtml">;
<head>
<title>Lijst medewerkers</title>

<script type="text/javascript">
var xmlHttp;
var name;
var title;
var department;
var deleteID;
var EMP_PREFIX = "emp-";

function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}

function addEmployee() {
name = document.getElementById("name").value;
title = document.getElementById("title").value;
department = document.getElementById("dept").value;
action = "add";

if(name == "" || title == "" || department == "") {
return;
}

var url = "EmployeeList.php?"
+ createAddQueryString(name, title, department, "add")
+ "&ts=" + new Date().getTime();

createXMLHttpRequest();
xmlHttp.onreadystatechange = handleAddStateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}

function createAddQueryString(name, title, department, action) {
var queryString = "name=" + name
+ "&title=" + title
+ "&department=" + department
+ "&action=" + action;
return queryString;
}

function handleAddStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
updateEmployeeList();
clearInputBoxes();
}
else {
alert("Error while adding employee.");
}
}
}

function clearInputBoxes() {
document.getElementById("name").value = "";
document.getElementById("title").value = "";
document.getElementById("dept").value = "";
}

function deleteEmployee(id) {
deleteID = id;

var url = "EmployeeList.php?"
+ "action=delete"
+ "&id=" + id
+ "&ts=" + new Date().getTime();

createXMLHttpRequest();
xmlHttp.onreadystatechange = handleDeleteStateChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}

function updateEmployeeList() {
var responseXML = xmlHttp.responseXML;

var status = responseXML.getElementsByTagName("status").item(0).firstChild.nodeValue;
status = parseInt(status);
if(status != 1) {
return;
}


var row = document.createElement("tr");
var uniqueID = responseXML.getElementsByTagName("uniqueID")[0].firstChild.nodeValue;
row.setAttribute("id", EMP_PREFIX + uniqueID);

row.appendChild(createCellWithText(name));
row.appendChild(createCellWithText(title));
row.appendChild(createCellWithText(department));

var deleteButton = document.createElement("input");
deleteButton.setAttribute("type", "button");
deleteButton.setAttribute("value", "Verwijder");
deleteButton.onclick = function () { deleteEmployee(uniqueID); };
cell = document.createElement("td");
cell.appendChild(deleteButton);
row.appendChild(cell);

document.getElementById("employeeList").appendChild(row);
updateEmployeeListVisibility();
}

function createCellWithText(text) {
var cell = document.createElement("td");
cell.appendChild(document.createTextNode(text));
return cell;
}

function handleDeleteStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
deleteEmployeeFromList();
}
else {
alert("Error while deleting employee.");
}
}

}

function deleteEmployeeFromList() {
var status = xmlHttp.responseXML.getElementsByTagName("status").item(0).firstChild.nodeValue;
status = parseInt(status);
if(status != 1) {
return;
}

var rowToDelete = document.getElementById(EMP_PREFIX + deleteID);
var employeeList = document.getElementById("employeeList");
employeeList.removeChild(rowToDelete);

updateEmployeeListVisibility();
}

function updateEmployeeListVisibility() {
var employeeList = document.getElementById("employeeList");
if(employeeList.childNodes.length > 0) {
document.getElementById("employeeListSpan").style.display = "";
}
else {
document.getElementById("employeeListSpan").style.display = "none";
}
}
</script>
</head>

<body>
<h1>Lijst medewerkers</h1>

<form action="#">
<table width="80%" border="0">
<tr>
<td>Naam: <input type="text" id="name"/></td>
<td>Positie: <input type="text" id="title"/></td>
<td>Afdeling: <input type="text" id="dept"/></td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="button" value="Toevoegen" onclick="addEmployee();"/>
</td>
</tr>
</table>
</form>

<span id="employeeListSpan" style="display:none;">
<h2>Medewerkers:</h2>

<table border="1" width="80%">
<tbody id="employeeList"></tbody>
</table>
</span>
</body>
</html>

EmployeeList.php
<?php
session_start();
$refresh = new EmployeeList();

//$refresh->sendHeaders();
$result = $refresh->getResults();
//echo $result;

//
class EmployeeList {
public $action;

public function __construct() {
$this->action = $_GET['action'];
}

function sendHeaders() {
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header('Content-type: text/xml');
}

function getResults() {
if($this->action == "add") {
$this->addEmployee();
}
else if($this->action == "delete") {
$this->deleteEmployee();
}

}

private function sendResponse($responseText) {
$this->sendHeaders();
echo $responseText;
}

protected function addEmployee() {

//Store the object in the database
$uniqueID = $this->storeEmployee();

//Create the response XML
$xml = "<result><uniqueID>";
$xml .= $uniqueID;
$xml .= "</uniqueID>";
$xml .= "<status>1</status>";
$xml .= "</result>";

//Send the response back to the browser
$this->sendResponse($xml);
}

protected function deleteEmployee() {
$id = $_GET["id"];
/* Assume that a call is made to delete the employee from the database */
//Create the response XML
$xml = "<result>";
$xml .= "<status>1</status>";
$xml .= "</result>";

//Send the response back to the browser
$this->sendResponse($xml);
}


private function storeEmployee() {
/* Assume that the employee is saved to a database and the
* database creates a unique ID. Return the unique ID to the
* calling method. In this case, make up a unique ID.
*/
$uniqueID = "";
// Random randomizer = new Random(System.currentTimeMillis());
for($i = 0; $i < 8; $i++) {
$uniqueID .= rand(0,9);
}

return $uniqueID;
}
}
?>
Ok mensen ik ben er al achter. Het komt omdat ik PHP4 gebruik volgens mij. Ik heb de privates en publics weggehaald in het PHP bestand. En het volgende veranderd
<?

class EmployeeList {
public $action;

public function __construct() {
?>
IN:

<?

class EmployeeList {
$action;

function EmployeeList() {
?>

Nu werkt het wel :)

Reageren