Jenny Dithe
09-09-2010, 11:31 AM
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.
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.