1

I need to call a PHP function in another file, but I'm getting device width with JavaScript, so I'm using JavaScript if statement, but it doesn't work as I expected.

<script>            
  var lWidth = window.screen.width;
  console.log(lWidth);

  if (lWidth <= 1024) {
    <?php 
      require_once('classes/Galerie.php');

      $galerie = new Galerie('uploads',3); //uploads is folder and 3 row of images
      $galerie->Nacti();
      $galerie->Vypis();
    ?>
  } else {                  
    <?php 
      require_once('classes/Galerie.php');

      $galerie = new Galerie('uploads',1);
      $galerie->Nacti();
      $galerie->Vypis();
    ?>
  }
</script>

Is it even possible in one file? Or should I use AJAX? I never worked with AJAX, so I dont know. Thanks.

Gasky09
  • 13
  • 5
  • What is the output of Nacti() and Vypis()? – Ne Ma Jul 02 '18 at 20:34
  • 6
    PHP runs on your server, JavaScript runs in the client browser. – Pointy Jul 02 '18 at 20:36
  • Neil Masters, Nacti() goes through directory and loads images Vypis() displays them – Gasky09 Jul 02 '18 at 20:39
  • 2
    AJAX it is. Time to read up on some tutorials. Might be beneficial to get familiar with jquery as well (since it makes ajax much much simpler). – IncredibleHat Jul 02 '18 at 20:42
  • Both PHP statements will be executed. PHP is executed on the server and the output (including the JavaScript) is sent to the browser and evaluated there. In other words: To the PHP runtime, the JavaScript code is just arbitrary text and completely ignored. Not sure if Ajax helps in your particular case. You may want to store the screen width in a cookie and read on the server. – Felix Kling Jul 02 '18 at 20:44
  • 1
    You need `Nacti()` and `Vypis()` to echo Javascript code that displays the gallery. Then it will put both sets of Javascript in the returned page, but only execute one of them depending on the screen width. – Barmar Jul 02 '18 at 20:53
  • You could use ajax or do a document.location.search or something like that in javascript, so as to send the lWidth to the php script. – Jelmer Jellema Jul 02 '18 at 21:52
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Jo. Jul 02 '18 at 22:34

0 Answers0