...

View Full Version : AJAX Call Not working



Jenny Dithe
09-09-2010, 12:31 PM
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:


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



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.

ashishchaudhary
09-18-2010, 07:41 PM
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




<?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.

A1ien51
09-18-2010, 09:04 PM
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

strakez
09-18-2010, 11:30 PM
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/



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum