galvin Posted May 19, 2011 Share Posted May 19, 2011 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=""; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/236821-code-working-on-page-but-not-via-an-included-file/ Share on other sites More sharing options...
Fadion Posted May 19, 2011 Share Posted May 19, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236821-code-working-on-page-but-not-via-an-included-file/#findComment-1217368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.