twomt
01-05-2008, 09:15 AM
Hi,
this is probably the most asked question of all those Ajax n00b's of which I gladly am one, but I really dont know what I am doing wrong here. I copied this example code and tried to use it but I am probably still doing something wrong.
Anyway, here is my index.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript" language="javascript">
function createRequestObject() {
var req;
// Check whether or not the window.XMLHttpRequest object
// is recognised by the browser
if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert('Problem creating the XMLHttpRequest object');
}
return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
function sendRequestGet(act) {
// Open PHP script for requests
http.open('get', 'test.php?act='+act);
http.onreadystatechange = handleResponse;
http.send(null);
}
// Start the PHP script using the POST method
function sendRequestPost(act) {
// Open PHP script for requests
http.abort;
http.open('post', 'test.php');
http.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
http.send('act='+act);
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
// Text returned FROM the PHP script
var response = http.responseText;
if(response) {
// UPDATE ajaxTest content
document.getElementById("ajaxTest").innerHTML = response;
setTimeout(ajaxTest,20);
}
}
}
function displayAPS( pilotID ){
sendRequestGet('pilotID');
}
displayAPS( 2 );
</script>
<div id="ajaxTest">This is the test message that should get replaced
</div>
</body>
</html>
Now I am trying to display the output of test.php into this ajaxTest div, this obviously doesn't work.... otherwise I won't be posting this.
My test.php does the following (very complicated code, only PHP experts will understand it.
<?php
echo("test");
?>
If I understood it all correct, the div's default text should get replaced by the output of the php script.
Can someone help me here?
this is probably the most asked question of all those Ajax n00b's of which I gladly am one, but I really dont know what I am doing wrong here. I copied this example code and tried to use it but I am probably still doing something wrong.
Anyway, here is my index.php :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript" language="javascript">
function createRequestObject() {
var req;
// Check whether or not the window.XMLHttpRequest object
// is recognised by the browser
if(window.XMLHttpRequest){
// Firefox, Safari, Opera...
req = new XMLHttpRequest();
} else if(window.ActiveXObject) {
// Internet Explorer 5+
req = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// There is an error creating the object,
// just as an old browser is being used.
alert('Problem creating the XMLHttpRequest object');
}
return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
function sendRequestGet(act) {
// Open PHP script for requests
http.open('get', 'test.php?act='+act);
http.onreadystatechange = handleResponse;
http.send(null);
}
// Start the PHP script using the POST method
function sendRequestPost(act) {
// Open PHP script for requests
http.abort;
http.open('post', 'test.php');
http.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
http.send('act='+act);
}
function handleResponse() {
if(http.readyState == 4 && http.status == 200){
// Text returned FROM the PHP script
var response = http.responseText;
if(response) {
// UPDATE ajaxTest content
document.getElementById("ajaxTest").innerHTML = response;
setTimeout(ajaxTest,20);
}
}
}
function displayAPS( pilotID ){
sendRequestGet('pilotID');
}
displayAPS( 2 );
</script>
<div id="ajaxTest">This is the test message that should get replaced
</div>
</body>
</html>
Now I am trying to display the output of test.php into this ajaxTest div, this obviously doesn't work.... otherwise I won't be posting this.
My test.php does the following (very complicated code, only PHP experts will understand it.
<?php
echo("test");
?>
If I understood it all correct, the div's default text should get replaced by the output of the php script.
Can someone help me here?