Forum Moderators: coopster

Message Too Old, No Replies

Strange include() behavior

         

Spiridian

2:19 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



Ok, I'm building a php site which uses axaj for navigation. it basically goes:

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

Habtom

3:21 pm on Oct 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is strange indeed.

Try the following:
include("$file");

include "$file";

Habtom

Spiridian

3:40 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



no change with either, still getting the "1"

eelixduppy

3:43 pm on Oct 4, 2007 (gmt 0)



Include does not return the string with the file contents, it returns boolean value depending on whether or not the file was successfully included. If you want to remove the 1 (meaning success) you don't have to echo
$content
or you should be using
file_get_contents
instead.

joelgreen

3:59 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



Do you have some "return $something;" in your include file?

[update] include() will return something different from 1/0 if it has return statement [/update]

Spiridian

4:09 pm on Oct 4, 2007 (gmt 0)

10+ Year Member



aah, thnaks :D

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.

eelixduppy

11:14 pm on Oct 4, 2007 (gmt 0)



I'm not sure that this is the best way to present your information as it only works when javascript is enabled. There aren't many browsers that don't support javascript, however, some people turn it off.

Other than that, the method that you use looks like a reasonable way of going about it.