Simple AJAX call makes brain hurt


#1

Okay, I’m an old dog learning new tricks. I want the simplest possible AJAX call to send a variable from javascript to PHP and I seem to be failing miserably.
For clarification:
1 - jQuery is NOT an option here, and
2 - The variable is for sample only - please don’t flake about “should be using CSS” or somesuch.

Basically, want to assign a session variable based on client data before (more or less) the actual page load. The AJAX bit doesn’t seem to be sending at all (nothing in the $_GET or $_REQUEST globals) and basically I’m a bit lost.

The AJAX scripty bit

<script> var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","index.php?x="+screen.width,true); xmlhttp.send(null); </script>

The page is http://test.netdcon.net.

Full index.php Code
[php]<?php
/* index.php, //
-------------------------------------------------------------------- */

// Start/resume a PHP session -----------------------------------------
session_start();

// ********************************************************************
// Output HTML Header
// ********************************************************************
print “\r”;
print “<html xmlns=“http://www.w3.org/1999/xhtml">\r";
print “\r”;
print " Simple AJAX Test\r”;
print " <meta http-equiv=“Content-Type” content=“text/html; charset=iso-8859-1” />\r”;
print " <meta http-equiv=“Content-Language” content=“en-US” />\r";
print " <meta http-equiv=“Window-Target” content="_top" />\r";
print " <meta name=“Robots” content=“NOINDEX, NOFOLLOW” />\r";
print “\r”;

// ********************************************************************
// Output HTML Body
// ********************************************************************
print “\r”;

print “\r”;

$_SESSION[‘xsize’] = $_GET[‘x’];

// Page Content -------------------------------------------------------

print “<div class=“content”>\r”;
print "

 $_SESSION “; print_r($_SESSION); print " 
\r”;
print "
 $_ENV “; print_r($_ENV); print " 
\r”;
print "
 $_SERVER “; print_r($_SERVER); print " 
\r”;
print "
 $_COOKIE “; print_r($_COOKIE); print " 
\r”;
print "
 $_REQUEST “; print_r($_REQUEST); print " 
\r”;
print "
 $_GET “; print_r($_GET); print " 
\r”;
print “\r”;

// Close Page ---------------------------------------------------------
print “\r”;
print “\r”;
// -------------------------------------------------------------------- ?>[/php]


#2

I don’t think you understand how HTTP and CGI requests work yet. PHP executes when the browser requests the URL, and so at that time the $_GET and $_REQUEST variables in PHP will not be affected by whatever happens in the browser while it renders the HTML content.

The HTML content from the AJAX call is being ignored in your code. If you were inspecting the response body you should see it shows the $_GET and $_REQUEST variables updated like you expect.

The next problem is that when the browser requests the URL to display it a second time, the session variable ‘xsize’ is wiped. That is because your PHP code so far does not check if $_SESSION[‘xsize’] has already been assigned a value. It is always set to $_GET[‘x’’], and that is only specified in the AJAX call, so other times PHP has a null value.


#3

That’s easily fixed with a quick “if isset() {}”.

So my question is: is there no way to “pre-fetch” the variable without reloading the page or doing a redirect?


#4

[quote=“netdcon, post:3, topic:61403”]So my question is: is there no way to “pre-fetch” the variable without reloading the page or doing a redirect?
[/quote]

Nope. The browser must have loaded an document in order to run the JavaScript code. The JavaScript code however can modify the existing document instead of loading a new one. Depending on what you need done, you can probably use an AJAX call to fetch data or HTML from PHP to use to modify the existing document.


#5

Blah. No easy magic bullet for me then - though I admit I was kinda skeptical about that direction anyways.

Many thanks for the confirmation :slight_smile: