1

I'm using the following code to get the screen resolution.

This works fine with WAMP server but when I put it on the remote Linux server, $height returns 0 .

HTML/PHP

<head>
    ....

    <script language="javascript">
        var y=window.screen.availHeight;
        var x=window.screen.availWidth;
        window.location.href = "index.php?height=" + y + "&width=" + x;
        break;

</head>
<?php
    $height=$_GET['height'];
    $height=intval($height);
    echo($height);
?>
Jeff Noel
  • 7,500
  • 4
  • 40
  • 66
ebin
  • 175
  • 1
  • 1
  • 12

3 Answers3

1

PHP is server side. It receives a request from your browser and renders the HTML/CSS/Javascript output before sending it back to your browser. Thus, any information about your browser size or screen size needs to be sent to the server as some kind of request, either when you first open the page, or after you've loaded the page. Sending a request to the server after you've loaded a page is called AJAX, and that's most probably how you'll have to do it.

http://w3schools.com/ajax/default.asp

Samuel Reid
  • 1,756
  • 12
  • 22
  • So why wouldn't changing the window location (as hackish as it might be) cause it to send the data (from JavaScript) to the (PHP) server? Also note: "This works fine with WAMP server but when I put it on the remote Linux server, $height returns 0" – user2246674 Jun 21 '13 at 17:38
  • To pass the values, @Abin attaches these query string parameters to the URL. – akonsu Jun 21 '13 at 17:38
  • @user2246674 It's possible that the difference has to do with the fact that we are getting the width and height outside of window.onload...so it's possible that the difference in servers is causing the height and width not to be reported immediately...weird, since JS is client side. Probably an idea from way out in left field. – Esaevian Jun 21 '13 at 17:43
  • @Esaevian - definitely not a server issue. Most likely there's some difference in code that hasn't been posted. @\SamuelReid - a much better link would be: http://developer.mozilla.org/en-US/docs/AJAX/Getting_Started - it doesn't include fancy demos but it's a much better starting point if the OP is unfamiliar with AJAX requests. – Joe Jun 21 '13 at 17:54
1

You may want to explain what you are trying to do with this webpage, give an overall explanation of passing in the height to php, because this page is not working at all. This is the execution flow that occurs with this web page.

  1. User enters index.php in address bar.
  2. PHP Server handles the request.
  3. The $_GET array will not contain a height parameter.
  4. $height will contain no value.
  5. calling intval($height) when $height is empty will return 0.
  6. The page will load, 0 will be displayed.
  7. JavaScript is executed.
  8. The URL will be constructed with the height and width parameters.
  9. Browser will throw an error: Uncaught SyntaxError: Illegal break statement
  10. Execution ends.
Rick Suggs
  • 1,582
  • 1
  • 15
  • 30
  • thank you for the explanation. Bro, then why it's working with WAMP?. Most of the time, running in WAMP gives output, but remoteserver sucks. – ebin Jun 21 '13 at 18:23
  • Before you give up, very carefully look over your code in WAMP and remote server, compare them side by side and be sure there are no differences. Then very carefully compare the url you are typing into the address bar in WAMP and remote server, and make sure there are no differences. – Rick Suggs Jun 21 '13 at 18:28
  • Yay, finally a sensible explanation! :D – user2246674 Jun 21 '13 at 18:36
0

You can do this via AJAX, but you will need to send the data to a different PHP file and do the processing there (which should work out alright, since PHP is processed on the server and not on your computer.

AJAX works like this:

  • In javascript/jQuery, put some value into a variable
  • Create an AJAX code block (I recommend using the $.ajax() structure at first)
  • In the AJAX code block, send the var-value pair to the PHP file
  • PHP file receives the variable-value, and does something with it
  • PHP can return some data (usually a string of HTML, like a table, etc)
  • Data from PHP is received in the SUCCESS function of the AJAX code block
  • Inside SUCCESS function, you can inject received code into the DOM

Here are a couple of examples (it's actually a LOT easier than it sounds/appears at first glance):

Sending value of dropdown to PHP page

Another basic AJAX example

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • I am going to leave the *exact same comment* as I did on the other answer: So why wouldn't changing the window location (as hackish as it might be) cause it to send the data (from JavaScript) to the (PHP) server? Also note: "This works fine with WAMP server but when I put it on the remote Linux server, $height returns 0" – user2246674 Jun 21 '13 at 17:40
  • Again *..why wouldn't changing the window location (as hackish as it might be) cause it to send the data (from JavaScript) to the (PHP) server?* – user2246674 Jun 21 '13 at 17:49
  • @user2246674 I haven't had much luck with trying to tack `?a=this&b=that` onto links (which is what `window.location.href` is simulating: a link click). You may be able to get away with using `window.location.replace`, but I'm not certain that'll fix anything. – Samuel Reid Jun 21 '13 at 18:04