A PHP variable filled by javascript?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Locked
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

A PHP variable filled by javascript?

Post by Bill H »

Consider this line:

Code: Select all

print("div class='fbox' style='top: 60px; left: $Wpx'>");
$W is an integer variable whose value is determined by the width of the viewer's screen. The only way I know of to get that width is javascript, and I have been trying for hours to find a way to put that value into the PHP variable from the javascript fucntion and it has defeated me. In this and other boards I find tons on how to transmit information from PHP to javascript, but I have found nothing on the reverse.

I've also tried not using a variable at all but a js function using document.write() and none of my several tries at that methodology has worked either. I am no expert on js, by any means, but I didn't think I was this stupid.

Perhaps this should have been in the client side forum. If so, please feel free to move it. I posted it here because I'm trying to work the solution into PHP code.

Can anyone point me in the right direction?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

yes it should have been in client side.

to answer your question:

JS is client side, therefore you can not pass variable information without sending information back to the server. This can be done with a form post, or a GET var passed in the url string. This leaves you two options:
1)capture the screen size with JS (only way I know how also) and submit it to the server using one of the methods I suggested above. if you decide to use this method, I suggest you capture the screen size on a prior screen and then when you get to the screen where you want the div sized, just output it with php.
2)write it out as you suggested with JS (this is route I'd go with as it won't require any additional page loads).
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

write it out as you suggested with JS
Yes, but how? I think my post indicated I have been trying to do that with no success.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

double post...see here - viewtopic.php?t=33887
Locked