Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

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)

Preferred Member

10+ Year Member

joined:July 28, 2003
posts: 468
votes: 0

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)

Senior Member from CA 

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

joined:Aug 29, 2003
votes: 0

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)

Preferred Member

10+ Year Member

joined:July 28, 2003
posts: 468
votes: 0

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 ; )

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members