Jump to content
Sign in to follow this  
rwhite35

Pass JS Variables without Output Buffering or XMLHttpRequest

Recommended Posts

Hey All,

Need to pass the current window width to a PHP class which creates a slide show presentation.  The presentation image sizes need calculated for all devices including mobile and smart phone (mobile web).  

 

Problem is, PHP is server side and doesn't care about the windows width.  I'm using Javascript to acquire the window width and pass that value to PHP - before calling the class that create the presentation.

 

My workaround is using Output Buffering:

<php
  ob_start(); //starts buffering
?>
<script>
  var wwidth = screen.width;
  document.write(wwidth): //output
</script>
<?php
  $windowwidth = ob_get_clean(); //works
  error_log("Window width is: ".$windowwidth);
  //continue on with the rest of the script
?>

I dislike ducking in and out of PHP like this.  And I'm not able to use Javascript XMLHttpRequest because I need the size before the class is called, on the current page.

 

Any alternative suggestions?

Thanks in advance

Edited by rwhite35

Share this post


Link to post
Share on other sites

What you have there isn't actually working: $windowwidth contains the Javascript code you wrote. It wasn't evaluated. Your script is simply outputting

Window width is: <script>
  var wwidth = screen.width;
  document.write(wwidth): //output
</script>
If the PHP must have the width then you must push the user through some other page before then. No way around it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.