Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-09-2010, 11:31 AM   PM User | #1
Jenny Dithe
Regular Coder

 
Join Date: Sep 2010
Posts: 457
Thanks: 212
Thanked 1 Time in 1 Post
Jenny Dithe is on a distinguished road
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.
Jenny Dithe is offline   Reply With Quote
Old 09-18-2010, 06:41 PM   PM User | #2
ashishchaudhary
New Coder

 
Join Date: Sep 2010
Posts: 27
Thanks: 2
Thanked 0 Times in 0 Posts
ashishchaudhary is an unknown quantity at this point
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 06:44 PM..
ashishchaudhary is offline   Reply With Quote
Old 09-18-2010, 08:04 PM   PM User | #3
A1ien51
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
A1ien51 will become famous soon enough
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]
A1ien51 is offline   Reply With Quote
Old 09-18-2010, 10:30 PM   PM User | #4
strakez
New Coder

 
Join Date: Sep 2010
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
strakez is an unknown quantity at this point
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/
strakez is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:35 AM.


Advertisement
Log in to turn off these ads.