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

    Setting a PHP variable with AJAX

    Hey,

    I want to use AJAX to set a PHP variable from a JavaScript variable.
    My code is

    Code:
    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();
    In the Ajax file getRoute.php I have

    PHP Code:

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

    require_once (
    'includes/mysql_connect.php');

    $r mysql_query("SELECT * FROM routes WHERE ".$q." ORDER BY ".$sort." LIMIT ".$limit);
    .....etc

    All the code from then on works and it displays the response text perfectly, but the sessions aren't registering for zoom and center.

    Any help guys?
    Thanks
    Last edited by crazyracky; 04-12-2011 at 06:41 PM. Reason: Correcting Code Tags

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    Why do you send variables that are going to be put into sessions to the server to be installed? Why don't you just do it before making the ajax call?


  •  

    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
    •