2

I need to know the screen size in jQuery or JavaScript and I need this value printed in PHP variable that will determine whether or not an item is displayed.

var width = $(window).width();

if (width <= 1024) {
   ///// How can I set a variable here and use it with PHP?
}

Thanks!

Mario Boss
  • 1,784
  • 3
  • 20
  • 43
Renata
  • 55
  • 8
  • php does not know what a screen is –  Nov 17 '15 at 23:55
  • 1
    PHP is a server side language that creates a web page and spits it out to the browser. The browser can not communicate with the process that created the page. Therefore, PHP can not react to any javascript while generating the page. You'll need to hide/show the item in javascript alone – Jaromanda X Nov 17 '15 at 23:56
  • To re-iterate a comment on an answer - if I understand the question, an item needs to be included/excluded based on page width. As php creates the page without knowing the page width, by the time javascript determines the page width, PHP has finished creating the page and is no longer in a position to include/exclude anything ... so, sending page width to PHP can only help when creating the NEXT page, not the current page – Jaromanda X Nov 18 '15 at 00:01

4 Answers4

0

mate, you never get the window width from your php code, php code is executing on your web server, you can only get your window size from javascript which is working on user's browser, you just need find a way sending the width you got from js to your php code, that's it !

sample code :

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "YOURPHPFILE.php?name=" + javascriptVariable; 
}
Kevin Simple
  • 1,225
  • 10
  • 22
  • if I understand the question, an item needs to be included/excluded based on page width. As php creates the page without knowing the page width, by the time javascript determines the page width, PHP has finished creating the page and is no longer in a position to include/exclude anything ... so, sending page width to PHP can only help when creating the NEXT page, not the current page – Jaromanda X Nov 17 '15 at 23:59
  • Thank you! I will try the way you said. – Renata Nov 18 '15 at 00:02
  • @Renata, cool, mate, dont forget to tick my answer, lol , happy coding! – Kevin Simple Nov 18 '15 at 02:16
0

I think I understand what you are asking but please tell me if I didn't get it correctly.

An idea that came immediately to mind is where you gather the viewport width and height information as soon as possible and then reload the page but pass the viewport information variables through the URL and receive them in PHP.

Try this code for the suggestion above. Normally, you put your script in an onload function but in this case, you want the dimensions as soon as possible.

<script>
var width = window.innerWidth;
var height = window.innerHeight;
location = location+'?w='+width+'&h='+height;
</script>

The second option is that you can do something complicated with AJAX. The third options is where you can use CSS media queries (as mentioned in the other answer[s]).

www139
  • 4,960
  • 3
  • 31
  • 56
0

If you just want to display elements based on the browser width, use CSS media queries and the display:none; property to hide elements.

flomei
  • 860
  • 1
  • 11
  • 36
  • [Stack Overflow says](https://stackoverflow.com/questions/5769493/ie8-support-for-css-media-query) that IE 8 or older will not understand media queries. Newer versions should do so thanks to CSS3 support. – flomei Nov 18 '15 at 00:08
  • so a (old IE specific) css/javascript alternative could be used for old IE using ` – Jaromanda X Nov 18 '15 at 00:10
  • I think so, yes. Although you might have to accept some drawbacks when using "modern" CSS. On the other hand, IE 8 and older is really dying, so you might consider not putting that work in. – flomei Nov 18 '15 at 00:13
0

As you have written in your question you have to check the correct width on the front-end side. You can use it jQuery or pure JS.

If you want to set some PHP variable and pass it to the PHP script then you have to use AJAX.

And again.. You can use it as a plain JS (XMLHttpRequest object) or ajax method from jQuery, like that:

JS code:

var variableForPHP = <?php echo 'some_value' ?>;

$.ajax({
   url: 'script.php',
   method: 'post',
   dataType: 'json',
   data: {phpVariable: variableForPHP},
   success: function (respondFromPHP) {
     // do something after AJAX call
     console.log('respond from PHP: ' + respondFromPHP);
   }
});

PHP code:

<?php
  $variableFromAjax = $_POST['phpVariable'];    
  // do something
  var_dump($variableFromAjax);
  $respondToAjax = 'thanks for variable: '. $variableFromAjax;
  echo json_encode($respondToAjax);
?>
Mario Boss
  • 1,784
  • 3
  • 20
  • 43