...

View Full Version : AJAX session value if else onload



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


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

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




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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum