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
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post

    AJAX session value if else onload

    Hi,

    I'm trying to get to grips with Ajax, I need to call different ajax files on onload depending upon the ajax session.

    I thought I would try and get the method working so simplified my attempts to a test page which once I have understood it I can apply to my website (in other words I do really need this) and would appreciate any assistance on how I should work this.

    So the main page goes:
    Code:
    <?php
    session_start();
    $_SESSION['ID']='x';
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    if ($_SESSION['ID']='x')
    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    }
    else 
    xmlhttp.open("GET","ajax_mine.txt",true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body onload="loadXMLDoc()">
    
    <div id="myDiv"><h2>Show Details</h2></div>
    
    </body>
    </html>
    Then ajax_mine.txt just says mine

    And ajax_info.txt contains "not mine".

    Sorry for bringing this back to basics, but I wrote all my code and then realised I was trying to run before I walk.

  • #2
    Regular Coder
    Join Date
    Sep 2010
    Posts
    460
    Thanks
    214
    Thanked 1 Time in 1 Post
    OK I have managed to change my code to the below which does actually show a response, except the if else does not seem to be working, in other words it goe straight to the ajax_mine.txt file, even though I have preset the session variable to x.

    I am completely stuck and can't move forward, I would really appreciate any help.

    Code:
    <?php
    session_start();
    $_SESSION['ID']='x';
    ?>
    <html>
    <head>
    <script type="text/javascript">
    if ($_SESSION['ID']=='x'){
    function loadXMLDoc()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    
    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    	}
    }
    
    else{
    function loadXMLDoc()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
    
    xmlhttp.open("GET","ajax_mine.txt",true);
    xmlhttp.send();
    	}
    }
    </script>
    </head>
    <body onload="loadXMLDoc()">
    
    <div id="myDiv"><h2>My profile</h2></div>
    
    </body>
    </html>


  •  

    Posting Permissions

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