Forum Moderators: coopster
you click on an a href=javascript:sndReq('test')
the ajax then looks like
---
var http = createRequestObject();function sndReq(action) {
http.open('get', 'content.php?action='+action);
http.onreadystatechange = handleResponse;
http.send(null);
}
so in content.php i've got:
---
<?php $path = "";
switch($_REQUEST['action']) {
case 'test':
$elementId = "content";
break;
}
$file = $path.$_REQUEST['action'].'.php';
$content = include($file);
echo $content;
?>
which goes to the ajax handler:
---
function handleResponse() {
if(http.readyState == 4){
var response = http.responseText;
var update = new Array(); if(response.indexOf('¦'!= -1)) {
update = response.split('¦');
document.getElementById(update[0]).innerHTML = update[1];
}
}
}
now, the issue is that when i use the include() function in content.php, whatever the contents of the file, it always get appended with "1" at the end. so in the case of test.php, if the file just contains "content¦test", the output on the screen will be "test1". where does the 1 come from?
if i replace include() with file_get_contents(), the "1" disappears, which leads me to belive it has something to do with php rather than the ajax. the problem with doing it this way, is that any php in the file doesnt get executed. any ideas?
thanks
$content or you should be using file_get_contents instead.
all good now. i've removed the echo line and it works perfectly.
one more question though, have i put the php/ajax together properly? what i mean is, has it been 'designed' well? at the moment, forms with
action="<?php $_SERVER['PHP_SELF'];?> don't work, and i'm assuming that its because PHP_SELF refers to index.php, rather than the page with the actual form where the php code is. much of this stuff is new to me, so before i spend a month learing things the wrong way, is there a better/smarter way to implement this?
thanks again.
Other than that, the method that you use looks like a reasonable way of going about it.