View Full Version : Load background image depending on dynamic variable

03-23-2009, 02:55 PM
Hi all,

I'm receiving an id using $_REQUEST["id"]; in a page called page.php
This id will be used to change the background image of page.php depending on its value. To do so I'm using:

$num = $_REQUEST["id"];

<body style='background-image: url(/images/";
if ($num == 63){
echo");width:100%;height:100%;text-align:center;margin:0 auto;'>....

The id changes every now and then and in all cases it is stored in $num = $_REQUEST["id"]; with no problem, works great, I've been testing it.
The issue is that once the background is loaded it does not change even if $num equals to 63.
Cache?? Should I preload the images? What can I do??

Thanks a ton!!

03-23-2009, 03:08 PM
I don't really see a reason why it shouldn't work, but doing it
like below might be an easier way? Use the ID as part of your background image filename.
So in the example below, if ID = 2 the style would be:
background: url(/images/background2.jpg)

$num = $_REQUEST["id"];

$style="background: url(/images/background".$num.".jpg); width:100%; height:100%; text-align:center; margin:0 auto; ";
<body style="<?=$style?>">

EDIT: "background.jpg" would be the default background if ID does not exist.


03-23-2009, 03:44 PM
Just a tip, I don't recommend using "id" it's a primary name for other things. Such as for users is id, and other tables like news would have id.

03-23-2009, 04:28 PM
I wouldn't use $_REQUEST either, but to each their own.

03-23-2009, 05:50 PM
Thanks to both for answering.
In both solutions I get the first background but when the id changes it does not change to the second background :(

Hope the following may give you a clue:
In Firebug's console:
The id is posted. The Post tab shows the id and its value correctly every time a new id is posted. Then in the Response tab for each id I have the correct html. If I copy and paste this html code into a new html document, it shows perfectly in the browser.
But that's in the Response Tab for each id posted.

Back in page.php, if I echo $num it shows in the Response tab and also changes every time to its new value coming from the newly posted id. But the echo does not show in page.php just in the Response Tab ... what's going on???
Happens using $_REQUEST["id"]; and using $_POST["id"]; (and also using echo or print)

Thanks once again!

03-23-2009, 06:01 PM
Copy/paste your whole page.php in here or if it's too big - in pastebin.com .

03-23-2009, 06:15 PM
steelaz, page.php is as simply as this:

$num = $_REQUEST["id"];
echo $num;
<body style='background-image: url(/images/";
if ($num == 63){
echo");width:100%;height:100%;text-align:center;margin:0 auto;'>.... the rest is just an iframe.

The iframe contains the page that is posting the id
It is posting the id using jQuery

var thepage = "page.php";
$.post(thepage,{"id":value dynamically coming from database});

Any ideas on how to catch this id in page.php?

03-23-2009, 06:31 PM
If your going to have a dozen background, store them in the database. If you are storing them in the database then it's super easy, just query for the background and display it. Doesn't require REQUEST, and for this name it "bgid" instead of "id"

03-23-2009, 06:39 PM
masterofollies, thanks, I know, but I'm afraid I cannot use database for this matter :((

C'mon, must be a way of getting that id (now I'm calling it "myid" just in case, thanks), it's there, it's already posted. Once I have it, changing the background is piece of cake..

03-23-2009, 07:16 PM
Can you post a link to the page? Its probably a caching issue but maybe not. Seeing the actually page might help us diagnose the problem.

03-23-2009, 08:03 PM
Good idea NancyJ.

Here's the page:

And a screenshot so you can see how myid it is echoed in the Response tab (also in the post tab shows myid and the value) but not in page.php:

If you click in another video, you'll see that myid changes but not the background..

Thanks everyone once again!

03-24-2009, 05:40 AM
You're using an <iframe> ... you didn't mention that before.
Anything you click within that <iframe> stays in the frame and
has no affect on the rest of your page. Your <body> has already
been loaded and now you're working inside the <iframe>.

Also, declare a content-type and other header information,
as not declaring puts the browser into "quirks" mode. Not really
part of your problem, but it will affect other things.

Just for fun, eliminate the <iframe> and see what happens.

03-24-2009, 10:45 AM
Cannot eliminate the iframe, the page inside of it is the one that is posting myid ...
So, any way of making myid jump from the iframe to the parent page?
Can I include the page inside of the iframe in the parent page using another formula?


03-24-2009, 01:28 PM
In order to avoid iframes I'm trying to use a php's include but it does not load the javascript of the included page ... has ever happpened to you? :confused: