Jump to content

code working on page, but not via an included file??


galvin

Recommended Posts

I have a snippet of code like this below.  If I have it directly on my page, it works fine.  But if I move this snippet of code into an include file and use "require_once('thefile.php');", the code no longer works properly.  How is that possible? FYI, this snippet is in the middle of a for loop on the regular page.  Maybe you can't use includes when in a loop? I don't know.  Makes no sense that it works when on the page, but not when included using require once.

 

Any thoughts how this could be possible?  I imagine there is some rule with include files that I'm missing???

 

This is the code and I don't think it matters exactly what its doing so I won't bother you with that.  This is how it looks on the page itself...

 

    if ($type=="one") {
		$_SESSION['cluenum']=1; //so clue #1 begins as the selected clue
		echo "<script type='text/javascript'>document.getElementById('clue1').style.border = '1px solid red';</script>";  //sets the styling on clue #1
		$allcode = "onclick='setnumber($i);'";
	} else {
	// type must be equal to all, so don't do anything special
		$allcode="";
	}


 

 

This is how it looks in the include file...

<?php
    if ($type=="one") {
		$_SESSION['cluenum']=1; //so clue #1 begins as the selected clue
		echo "<script type='text/javascript'>document.getElementById('clue1').style.border = '1px solid red';</script>";  //sets the styling on clue #1
		$allcode = "onclick='setnumber($i);'";
	} else {
	// type must be equal to all, so don't do anything special
		$allcode="";
	}

?>

 

 

 

Included PHP files work as if they were actual PHP code in the same page, inheriting scope and the ability to use variables defined before the inclusion. So, the behavior, as shallow as you describe it, makes no sense.

 

I guess you're sure the path to the included file is correct, so try printing any of the variables to see if there's any output. Also, what do you mean by "no longer works properly"? It doesn't work at all, or not in the way you expect it to?

 

PS: There's no need to achieve what you're trying with Javascript. You can create a CSS class (ex: .red-border { border:1px solid red; }) and apply it to the elements. It will be faster and accessible to anyone.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.