473,513 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i'm not able to get ajax response value

121 New Member
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
5 2386
gits
5,390 Recognized Expert Moderator Expert
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
sbettadpur
121 New Member
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
5,390 Recognized Expert Moderator Expert
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
sbettadpur
121 New Member
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
5,390 Recognized Expert Moderator Expert
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

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

Similar topics

25
2753
by: meltedown | last post by:
This is supposed ot be an example: http://www.ajaxtutorial.net/index.php/2006/11/30/simple-ajax-using-prototype-part-2/ It says : This example is probably the simplest example you will ever...
10
4829
by: trpost | last post by:
I am using ajax / php where I am looking up some info from the database and populating a select list dynamically, however I am running into some sort of size limitation with the ajax.response...
3
3120
by: equazcion | last post by:
Hi, I have an image reference (IMG) in my page that changes depending on the value of a database field. Clicking the image triggers an Ajax call to change the database field (toggles the field...
1
4013
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
13
2122
by: adam | last post by:
Hey All, I'm relatively new to all this and any help would be appreciated. What I'm aiming to do is create a few requests, to 1. Search for a Student against XML created from the database 2. If...
6
7933
by: sheldonlg | last post by:
I came across a problem and googling for an answer didn't help. What I want to do is run an AJAX script that sets a hidden variable on the form. I call the AJAX script from a javascript...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5689
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.