1

I have a small script that returns the width and height of my Window, what I need is to save the result to a PHP session variable.

My script

var viewportwidth;
var viewportheight;


if (typeof window.innerWidth != 'undefined')
{
   viewportwidth = window.innerWidth,
   viewportheight = window.innerHeight
}

like

$_SESSION['w'] = viewportwidth
$_SESSION['h'] = viewportheight

Is this possible?

1
  • @Epodax is correct you need to use Jquery Ajax for it Commented Dec 1, 2015 at 9:40

3 Answers 3

1

you Can easily get this done using Ajax

JS / Client Side code

$().ready(function(){
  $.ajax({
    method  : "POST",
    url     : "capture.php",
    data    : { height: window.screen.availHeight, width: window.screen.availWidth }
  })  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

And for Server Side it will be Like

session_start()

$_SESSION['h'] = $_REQUEST['height'];
$_SESSION['w'] = $_REQUEST['width'];
Sign up to request clarification or add additional context in comments.

Comments

0

You can try something like this :

  var viewportwidth;
  var viewportheight;

  $.ajax({
      type: "POST",  
      url: 'fileupload.php',
      data: viewportwidth,
      processData: false,
      // ... Other options like success, error and etc
  });

You can also create a variable which includes a complete string and pass that variable in 'data' tag.

Comments

0
$.ajax({
          type: "POST",
          url: "/sample.php",
          data: { width: viewportwidth,height:viewportheight }
        }).done(function( msg ) {
           //your response code here
}

in sample.php

<?php 
    $_SESSION['w'] = $_POST['width'];
    $_SESSION['h'] = $_POST['height'];
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.