By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,743 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

i'm not able to get ajax response value

100+
P: 121
Hello everybody

I have some issues in my program.

Let me explain my requirement briefly, i am using php, mysql, javascript and ajax for my application.

when my application opens means if user logs into application automatically a pop up window should come, means i don't want for every time that should open, For specific condition only pop up window will open.

Let me explain that condition i have table called interview and field called as Interview_date and time. If interview_date is current date and interview time difference with the current time is less than 2 hours or greater than 2 hours. Then only pop window should open.

Through XmlHttp request i can able to fetch interview date and time from database of that particular user (means who logs in), through xmlHttpr.responseText i can able to catch interview date and time.

but i can't able to return I_date and I_time from outside of function.
below i am showing my source code

[HTML]<html>
<head>

<script type='text/javascript'>


function cal_time()
{
var logged_in_user='{$_SESSION[valid_user]}';

date1 = new Date();
date2 = new Date();
diff = new Date();

var theyear=date1.getFullYear()
var themonth=date1.getMonth()+1
var thetoday=date1.getDate()

var curr_time = date1.getTime();
var curr_hour = date1.getHours();
var curr_min = date1.getMinutes();

if(curr_hour >= 12)
{
if(curr_hour == \"12\")
{
meridian = \"PM\";
} else {
curr_hour = curr_hour - 12;
meridian = \"PM\";
}
}else {
meridian = \"AM\";
}
var tot_time = themonth + '/' + thetoday + '/' + theyear + ' ' + curr_hour + ':' + curr_min + ' ' + meridian;



var xmlHttpr;

xmlHttpr=GetXmlHttpObject()

if (xmlHttpr==null)
{
alert (\"Browser does not support HTTP Request\")
return
}

var url=\"http://localhost/recruitmentsolution/auto.php\";
url=url+\"?c=\"+logged_in_user;

xmlHttpr.onreadystatechange=getusername;
xmlHttpr.open(\"GET\",url,true)
xmlHttpr.send(null)


function getusername()
{


if (xmlHttpr.readyState==4 || xmlHttpr.readyState==\"complete\")
{
usernamestring=xmlHttpr.responseText
userarr = usernamestring.split(\"!\");
nm = userarr[0];

var nm2 = userarr[1];


var nm3 = nm2.split(\"-\");
tot = nm3[1] + '/' + nm3[2] + '/' + nm3[0];


finall = tot + ' ' + nm;

return(finall);

}


}


function GetXmlHttpObject()
{
var xmlHttpr=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttpr=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
{
xmlHttpr=new ActiveXObject(\"Msxml2.XMLHTTP\");
}
catch (e)
{
xmlHttpr=new ActiveXObject(\"Microsoft.XMLHTTP\");
}
}
return xmlHttpr;
}



var ttemp_tot_time = getusername();

var t_tot_time = ttemp_tot_time;

var a = tot_time;
var b = t_tot_time;


date1temp = new Date(a);
date2temp = new Date(b);

date1.setTime(date1temp.getTime());
date2.setTime(date2temp.getTime());

// sets difference date to difference of first date and second date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);
days = Math.floor(timediff / (1000 * 60 * 60 * 24));
timediff -= days * (1000 * 60 * 60 * 24);
hours = Math.floor(timediff / (1000 * 60 * 60));

timediff -= hours * (1000 * 60 * 60);
mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);
secs = Math.floor(timediff / 1000);
timediff -= secs * 1000;

if(days < 1 && t_tot_time != 'undefined')
{
if((hours == 1 && (mins == 58 || mins ==59) ) ||(hours == 2 && (mins == 0 || mins == 1)))
{

popWindow();
var howLong = 4000;
t = null;
t = setTimeout(\"newWindow.close()\",howLong);
} else {

return false;
}
}

}
</head>
<body onload='cal_time();'>

</body>
</html>[/HTML]



I am calling getusername() function to get Interview_Date and Interview_Time values and that will store into ttemp_tot_time variable. But the function getusername() is not returning any value its returning a null value. If i alert the value of variable finall it's showing the value.

please help me in this part to get date and time value, so that i can calculate the difference and i can open pop up window.

i am waiting for your reply.

Thankyou
Jul 21 '08 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,343
in your current 'program-flow' you rely on the result instantly ... but the response is ready when the onreadystatechange is fired with readystate == 4 ... so you have to ensure that your program is processed when this is done and not before ...

kind regards
Jul 23 '08 #2

100+
P: 121
Thanks for your reply,

i am not able to catch up your points, please can u explain me briefly where i am doing mistake
Jul 25 '08 #3

gits
Expert Mod 5K+
P: 5,343
in your line 109 you call the function get_username() ... but this is/should be the callbackfunction for the request ... you already assigned it to the onreadystatechange property of your current request. actually there are 2 times where this function is called ... the first place (line 52) and the mentioned line 109 ... while the call of 109 seen from the data-flow is BEFORE the call in 52 ... so everything that needs the request's response should be called from inside your get_username()-function so that you could be sure you HAVE the response received before ... this is just the paradigm of async flow :)

kind regards
Jul 25 '08 #4

100+
P: 121
hi,

Thanks a lot ur solution has solved my problem.

Again Thank you very much

I will explain u what i did

i put entire code starting from line number 109 to 154 into function getusername();

now everything is working

Thanks
Jul 25 '08 #5

gits
Expert Mod 5K+
P: 5,343
glad to hear that you got it working :) ... for better code reuse you could just put the moved code to a new function and call that from the get_username() function ...

post back to the forum anytime you have more questions ...

kind regards
Jul 25 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.