Page 1 of 1

A PHP variable filled by javascript?

Posted: Wed Jun 01, 2005 12:30 am
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?

Posted: Wed Jun 01, 2005 12:47 am
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).

Posted: Wed Jun 01, 2005 1:10 am
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.

Posted: Wed Jun 01, 2005 3:12 am
by JayBird
double post...see here - viewtopic.php?t=33887