I am getting a screen resolution through XMLHttpRequest. I am opening a show_content.php file with xmlhttp.open that contains screen resolution variables which I pull with the GET Method.

 xmlhttp.open("GET","show_content.php"+queryString,true); 

This works!

and showing this file with the variables in a following div

<div id="txtResolution"></div>

This part works as well!

PROBLEM: This div ( show_content.php ) contains screen resolution variables that I would like to use throught the rest of the code outside of it but I can not pass them somehow. How can I get those variables to be passed to the original code.

I used the GET method but can not pass the variables to the original code outside the show_content.php I have tried with GLOBALS and Sessions as well but no luck

Here ist the code. Everything works properly but I can not pass the variables from the "txtResolution" div to the rest of the code

function showResolution(field_id)
{
var xmlhttp;
if (field_id.length==0)
{ 
 document.getElementById("txtResolution").innerHTML="";
 return;
 }
if (window.XMLHttpRequest)
 {// code for IE7+, Firefox, Chrome, Opera, Safari
 xmlhttp=new XMLHttpRequest();
 }
else
 {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
 {
 if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtResolution").innerHTML=xmlhttp.responseText;
}
    }

var winW = 630, winH = 460;
if (document.body && document.body.offsetWidth) {
 winW = document.body.offsetWidth;
 winH = document.body.offsetHeight;
}
if (document.compatMode=='CSS1Compat' &&
document.documentElement &&
document.documentElement.offsetWidth ) {
 winW = document.documentElement.offsetWidth;
 winH = document.documentElement.offsetHeight;
}
if (window.innerWidth && window.innerHeight) {
 winW = window.innerWidth;
 winH = window.innerHeight;
 }
var queryString = "?width=" + winW + "&height=" + winH;

 xmlhttp.open("GET","show_content.php"+queryString,true);
 xmlhttp.send();
 }

Recommended Answers

All 3 Replies

This looks good. Show PHP code where you try to set variables to global or session.

<?php
session_start;

$height=$_GET['height'];
$width=$_GET['width

/////// session could not pass the variable
$_SESSION['width']=$width;

/////// GLOBALS  can not pass the variable
$GLOBALS['width']=$width;
?>
$height=$_GET['height'];
$width=$_GET['width'];

Your code raise PHP error if variable not set - must be check if(isset($_GET['height'])){ $height=$_GET['height']; } or much better you can use filtered variables PHP - filter_input

$height = filter_input(INPUT_GET, 'height', FILTER_VALIDATE_INT);
$width = filter_input(INPUT_GET, 'width', FILTER_VALIDATE_INT);

Then set as session variables only if variable is not NULL

if($width !== NULL){
    $_SESSION['width'] = $width;
}
if($height !== NULL){
    $_SESSION['height'] = $height;
}

Otherwise your session variables always set to NULL if not set new values on each http request - in fact it does not work as a session variables

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.