Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

AJAX request to set PHP session variable

Posted on 2011-10-16
6
Medium Priority
?
231 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:dolan2go
  • 5
6 Comments
 
LVL 11

Accepted Solution

by:
maeltar earned 2000 total points
ID: 36976487
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
 
LVL 1

Author Comment

by:dolan2go
ID: 36976623
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
 
LVL 1

Author Comment

by:dolan2go
ID: 36976661
After adding an 'alert' to the add_ses_var() function, it appears the function is not being executed (no alert).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:dolan2go
ID: 36976672
Now the alert is working.

However Live HTTP Headers is NOT reporting a GET of the AJAX page.
0
 
LVL 1

Author Comment

by:dolan2go
ID: 36976780
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
 
LVL 1

Author Closing Comment

by:dolan2go
ID: 36976785
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question