Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP Sessionwont set inside AJAX Script

    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 Code:
    <?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??

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Does this work properly and as intended when you attach directly to this script without the use of javascript?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Code:
    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...

    Code:
    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!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #9
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by crazyracky View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #11
    New Coder
    Join Date
    Mar 2011
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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???


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •