...

View Full Version : PHP Sessionwont set inside AJAX Script



crazyracky
04-12-2011, 11:22 PM
I have an Ajax script which I know is working because its result text is correct. I tried to add a couple more parameters to it by using PHP's session variables, unfortunately it isnt working.

Code is


<?php
session_start();
$_SESSION['zoom'] = $_GET["zoom"];
$_SESSION['center'] = $_GET["center"];
$q = $_GET["q"];
$page = $_GET["page"];
$limit = $_GET["limit"];
$sort = $_GET["sort"];
...etc


the $q part is for the rest of the script but I cant get zoom and center to be set right.

Any ideas??

Fou-Lu
04-12-2011, 11:24 PM
Does this work properly and as intended when you attach directly to this script without the use of javascript?

crazyracky
04-12-2011, 11:45 PM
Im unsure of what you mean im a bit new to all of this. Basically I want to assign a javascript variable to PHP and im using AJAX to do it.

Does that sound right?

Fou-Lu
04-12-2011, 11:50 PM
No, PHP isn't directly usable between the server and client. You may print a PHP variable into a javascript variable, or you may parse a PHP result from a full script into a javascript variable using ajax, but JS cannot directly talk to PHP.

Post the AJAX that you are using to call this script.

crazyracky
04-13-2011, 12:03 AM
var xmlhttp = getXHR();

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("display_routes").innerHTML=xmlhttp.responseText;
}
}

xmlhttp.open("GET","getRoute.php?q="+whereClauseString+"&page="+resultsPage+"&limit="+resultsLimit+"&sort="+sortBy+"&zoom="+currentZoom+"&center="+currentCenter,true);
xmlhttp.send();


getXHR() function further up the page...



function getXHR()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}


I have a map that changes center and zoom, and I need those variables in PHP further down the body of the page.

Thanks for your help so far!

Fou-Lu
04-13-2011, 12:31 AM
Here's where you have lost me. The PHP variables cannot be used further down the page without the assistance of ajax. You need to use JS to manually alter the data that its to be replaced with.
I think that's what you're looking to do. The use of sessions themselves are limited since they can only be retrieved by PHP. While you can echo them back to your JS or send them back and retrieve them from AJAX, they will not automatically update anything on the page.

crazyracky
04-13-2011, 12:56 AM
Hmm little lost too...
I assumed I could alter variables in JS, send AJAX with JS variables in URL, GET them as PHP session variables in AJAX file, then back on the original page I could assing the session variables to PHP variables.

What do you mean "manually alter the data"?

can you suggest an example solution to help me get my head round this!?

Cheers

Fou-Lu
04-13-2011, 04:46 PM
What I mean is HTTP is a stateless protocol. So when you set a variable on your HTML page either a direct print or JS or whatever using PHP, its not bound in any way to the server. When you alter the server variable, it will not be reflected by the client without using something like JS or a reload of the script to show the changes.
AJAX itself is simply calling a PHP page in the background, as are languages such as FLEX. The session will be applied when AJAX called that script, but any time you've printed a session variable in the current script it will not be updated by this change until the dom is manipulated by Javascript to update it. This requires PHP to return the results as a string (or text based such as XML) so that its accessible by Javascript.
Server side languages can generate anything they want when they send a page in things like Javascript. This is because PHP doesn't know what JS is, so its just text. On the other hand, client languages cannot be used to modify PHP variables or code, the only way to do this is to send a request to PHP including the variables to be handled to a PHP script, and use PHP to process the given request.

So when you use AJAX, you must handle the response that PHP sends in order to show the new data. AJAX's job will be to connect to a PHP script, give it some data to handle, then take the response from the PHP script and parse it accordingly. This can be as simple as a returned boolean, to a full out XML result of all new alterations. AJAX can then take this data, and use the JS dom to alter the current HTML to show the new data.

crazyracky
04-13-2011, 05:32 PM
Ok that makes sense..... I think. Sessions are out of the question then!

So I need to use the response text. Does this mean If all the script does is GET the variable, then echo(variable) inside the script, and then the response text would be that variable?

Could you give me a code example that takes the response text and sets it as a php variable?


Thanks

Fou-Lu
04-13-2011, 05:49 PM
Ok that makes sense..... I think. Sessions are out of the question then!

So I need to use the response text. Does this mean If all the script does is GET the variable, then echo(variable) inside the script, and then the response text would be that variable?

Thats correct. You can send it over in many different ways, standard text, may comma separated, XML, etc. I personally like using XML with AJAX in specific since once you get used to the dom its quite easy to pop and replace entire nodes instead of just text.



Could you give me a code example that takes the response text and sets it as a php variable?


Thanks

Nope, you can't do that. The response text is what's given back to AJAX, it cannot be used to replace a PHP variable since its worked with on the client side. HTML wise, the client has no idea what is and is not a result of a PHP variable.

crazyracky
04-13-2011, 06:04 PM
Perhaps im looking at this in the wrong way...

First thing im echoing links out on to my page. I want to add a JS variable to the link (...php?zoom=variable&center=variable) in there for current map zoom and center which are JS variables already.

Secondly I do a SQL query to find out how many results are queried so I can make page number links.

I definitely need the AJAX for the page numbers right? What about the first thing? Am I tryting to use server side when it is unneccesary???



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum