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

    AJAX Call Not working

    Hi,

    I am trying to use AJAX and thought I had used the w3 tutorials perfectly, but nothing works and I have clearly failed to grasp some basic element.

    What I want to do is I have multiple AJAX requests, so I thought I would have an external page which will handle all my ajax requests and then pull the right information and display it in the place holder.

    To keep things simple I will only post about my first request:

    I have a list of profiles and if you click on a profile I want a request to go to my ajax page to pull up and display the item information, different levels of information comes up depending on if you click on your profile or someone elses. Clicking on the profile takes you to a new page.

    That pages code is:
    Code:
    <?php
    	session_start();
    
    $_SESSION['Profilename']=='$clickedprofile';
    $_SESSION['MyProfile']=="$username"
    
    ?>
    <html>
     <head>
    <script type="text/javascript" src="myajaxpage.js"></script>
    </head>
    <body onload="showProfile()">
    
    
    <div id="txtHintThisProfile"></div>
    
    
    </body>
    </html>
    The myajax page code is:

    Code:
    var xmlhttp
    
    function ShowProfile()
    	{
    	if ($_SESSION['Profilename']=='$_SESSION['MyProfile']){
    
    		{
    		document.getElementById("txtHintThisProfile").innerHTML="";
    		return;
    		}
    	xmlhttp=GetXmlHttpObject();
    	if (xmlhttp==null)
    		{
    		alert ("Your browser does not support XMLHTTP!");
    		return;
    		}
    	var url="getmyprof.php";
    	url=url+"?q="+str;
    	url=url+"&sid="+Math.random();
    	xmlhttp.onreadystatechange=stateChanged;
    	xmlhttp.open("POST",url,true);
    	xmlhttp.send(null);
    }
    
    	elseif ($_SESSION['Profilename'] !='$_SESSION['MyProfile']){
    		{
    		document.getElementById("txtHintThisProfile").innerHTML="";
    		return;
    		}
    	xmlhttp=GetXmlHttpObject();
    	if (xmlhttp==null)
    		{
    		alert ("Your browser does not support XMLHTTP!");
    		return;
    		}
    	var url="getprof.php";
    	url=url+"?q="+str;
    	url=url+"&sid="+Math.random();
    	xmlhttp.onreadystatechange=stateChanged;
    	xmlhttp.open("POST",url,true);
    	xmlhttp.send(null);
    	}
    	}
    
    function stateChanged()
    	{
    	if (xmlhttp.readyState==4)
    		{
    		document.getElementById("txtHintThisProfile").innerHTML= xmlhttp.responseText;
    		}
    	}
    
    function Get XmlHttpObject()
    	{
    	if (window.XMLHTTPRequest)
    		{
    		return new XMLHttpRequest();
    		}
    	if (window.ActiveObject)
    		{
    		return new ActiveXObject("Microsoft.XMLHTTP");
    		}
    	return null;
    	}
    My two php documents have the information I want to call. It is clear to me I have got something very wrong here, but I am not sure what it is. Any help would be much appreciated.

  • #2
    New Coder
    Join Date
    Sep 2010
    Posts
    27
    Thanks
    2
    Thanked 0 Times in 0 Posts
    hay, first on all $_SESSION variable is of php script variables and you are using them in javascript then how they will be recognized,
    In your myajax file the line is not correct -

    if ($_SESSION['Profilename']=='$_SESSION['MyProfile']){

    but don't worry just do this in your myajax file --
    first store the result of session variables in javascript variables like

    Code:
    <?php session_start(); ?>
    
    var profilename = "<?php echo $_SESSION['Profilename']; ?>";
    var myprofile= "<?php echo $SESSION['MyProfile']; ?>";
    now in your if statement just do this

    if (profilename==myprofile){

    I think this would work.
    Last edited by ashishchaudhary; 09-18-2010 at 07:44 PM.

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    You are using the same global variable so you keep writing over the previous request.

    Use an library to make the Ajax calls and you will not have the issues.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    New Coder
    Join Date
    Sep 2010
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by A1ien51 View Post
    Use an library to make the Ajax calls and you will not have the issues.
    Eric
    jQuery.ajax() works pretty well for this:
    http://api.jquery.com/category/ajax/


  •  

    Posting Permissions

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