AJAX request to set PHP session variable

I'm actually searching for a way to put a variable into the onclick to reuse the function. The session variable 'travel_2' is NOT being set. What am I missing here?
<input class="btn" type="submit" value=" Do-it For Me " onclick="add_ses_var()" />

Open in new window

<script type="text/javascript">
function add_ses_var()
{
	var xmlhttp = false;
	if (window.XMLHttpRequest)		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	else		// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	xmlhttp.open ( "GET", "session_var.php?key=travel_2", true );
	xmlhttp.send();
}
</script>

Open in new window

session_var.php
session_start ( );

if ( !empty ( $_GET['key'] ) )
{
	$v = $_GET['key'];
	$_SESSION[$v] = 'good';
}

Open in new window

LVL 1
dolan2goAsked:
Who is Participating?
 
maeltarCommented:
ensure you have at to top of every page
<?
session_start();

Open in new window


Note, no white space, and it MUST be on each page..
0
 
dolan2goAuthor Commented:
Yes... that's on the top of the pages in question.

This is a new issue. Only the session_var.php file is new. Sessions are working, just fine.

Any ideas about the JavaScript and AJAX?
0
 
dolan2goAuthor Commented:
After adding an 'alert' to the add_ses_var() function, it appears the function is not being executed (no alert).
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
dolan2goAuthor Commented:
Now the alert is working.

However Live HTTP Headers is NOT reporting a GET of the AJAX page.
0
 
dolan2goAuthor Commented:
Found the problem. Evidently white spaces is not welcome in:
xmlhttp.open ( "GET", "session_var.php?key=travel_2", true );

Open in new window

Took out ALL the spaces and it works.
0
 
dolan2goAuthor Commented:
Your suggestion of white space triggered something...

When I removed from the javascript, it works.

Thank you for having the vision!! Even if it was in a different direction. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.