473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

function / return help

Hello,

PHP programmer in need of some js help. I can't seem to get this
function to return the value of xmlhttp.responseText.
alert(xmlhttp.responseText) gives me the value I'm looking for. I
assume this has to do scope... but don't quite get how to get the return
value in the `= function()` syntax.

function phj_func(url) {
xmlhttp.open("GET", url)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return(xmlhttp.responseText);
}
}

xmlhttp.send(null);

}

This was taken from:
http://www.webpronews.com/webdevelop...POnTheFly.html

Thanks!
Jul 23 '05 #1
5 4169


Jason Morehouse wrote:

I can't seem to get this
function to return the value of xmlhttp.responseText.
alert(xmlhttp.responseText) gives me the value I'm looking for. function phj_func(url) {
xmlhttp.open("GET", url)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
return(xmlhttp.responseText);
}
}


The onreadystatechange handler is a function of its own that is called
but XMLHttpRequest object when the readyState changes, the phj_func call
is already finished when the handler is called so obviously you can't
return any value in phj_func that stems from the onreadystatechange
event handler.
You need to call another function and pass the responseText text e.g.

xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
functionName(xmlhttp.responseText);
}
}
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Jason Morehouse wrote:
Hello,

PHP programmer in need of some js help. I can't seem to get this
function to return the value of xmlhttp.responseText.
alert(xmlhttp.responseText) gives me the value I'm looking for. I
assume this has to do scope... but don't quite get how to get the return
value in the `= function()` syntax.


Sorry, let me retry that...

var r = false;
function phj_func(url) {
xmlhttp.open("GET", url)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
r = xmlhttp.responseText);
}
}
xmlhttp.send(null);
return r;
}
Jul 23 '05 #3
Martin Honnen wrote:
The onreadystatechange handler is a function of its own that is called
but XMLHttpRequest object when the readyState changes, the phj_func call
is already finished when the handler is called so obviously you can't
return any value in phj_func that stems from the onreadystatechange
event handler.
You need to call another function and pass the responseText text e.g.


Cheers. Is there a way though to have the data returned to the function
called. Ie:

str = phj_func(url)?

Something like:

function phj_func(file,func,args) {
var r = false;
function got_data(str_data) {
r = str_data;
}
xmlhttp.open("GET", url)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
got_data(xmlhttp.responseText);
}
}

xmlhttp.send(null);
return (r);
}

though... that doesn't work.
Jul 23 '05 #4


Jason Morehouse wrote:
Martin Honnen wrote:
The onreadystatechange handler is a function of its own that is called
but XMLHttpRequest object when the readyState changes, the phj_func
call is already finished when the handler is called so obviously you
can't return any value in phj_func that stems from the
onreadystatechange event handler.
You need to call another function and pass the responseText text e.g.

Is there a way though to have the data returned to the function
called.


No, as said the function call to phj_func simply sets up an
onreadystatechange event handler, then calls the send method and exits,
it is already finished before the onreadystatechange event handler is
called for the first time.

There is some sort of event based programming in PHP too, for instance
if you look at the XML parser functions using Expat, there you set up
event handlers like the element_handler and then those are called when
elements are parsed.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5
Martin Honnen wrote:
There is some sort of event based programming in PHP too, for instance
if you look at the XML parser functions using Expat, there you set up
event handlers like the element_handler and then those are called when
elements are parsed.


Ah, of course. Thank you!

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Fine | last post by:
No joy on Macromedia's boards after a few days; maybe someone can help me here. I got an excellent string handling function off of planet-source-code.com that converts text strings to proper...
5
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
8
by: Johno | last post by:
I have written the two associated base classes below (Digital_camera and Review) to manage digital camera and review objects. They are base classes for which other derived classes can be written to...
5
by: elsenraat_76 | last post by:
Hello! I was wondering if someone could help me out with a problem I'm having? I'm trying to input a javascript value into an anchor tag (from a function), but don't have an event to call the...
4
by: bluedolphin | last post by:
This is my first function in Visual and I'm having a simple syntax syntax issue that I'm hoping someone can help correct for me. I have a function Public Function...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
4
by: Tony Lownds | last post by:
(Note: PEPs in the 3xxx number range are intended for Python 3000) PEP: 3107 Title: Function Annotations Version: $Revision: 53169 $ Last-Modified: $Date: 2006-12-27 20:59:16 -0800 (Wed, 27 Dec...
1
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.