Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

write 'back' a variable needed earlier in a page?

printing a variable created in an included page

12:16 pm on Nov 18, 2013 (gmt 0)

10+ Year Member

This is perhaps a (very) silly question.

I have a php page with javascript, and further down the page I 'include' another php script that generates a series of boxes. I need to pass the id's of those boxes into the javascript that will execute on page load.

The thing is, if I 'echo' the variable required ~before~ the 'included' php script, I get an 'undefined var' error. I could write the javascript after the php 'include', but the javascript must stay in the head because it executes on load.

What obvious thing am I overlooking? This is driving me crazy.

3:21 pm on Nov 19, 2013 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

This is very much a JavaScript question, not a PHP question.

two ideas for you.
1) trigger the javascript on a "domready" event, or on page load.
2) put the script at the end of the page.

in jQuery, it's like this:

// the page is loaded, so you can use a selector
// to find the ids on the page

because it executes on load.

I there's a misunderstanding there. If the page is loaded, the elements with their IDs will exist. Also, you don't have to have things in the <head> in order for them to execute on load.
6:29 am on Dec 2, 2013 (gmt 0)

10+ Year Member

Sorry in the lateness of my reply, and thanks for your answer.

Yes, that was about it, I was overthinking the question. In fact, I don't quite remember now ~what~ I was trying to do ; P

Right, I wanted to pass the (dynamically-generated) id's of boxes to the 'execution' script - javascript was indeed the answer. Thanks again ; )

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month