/    Sign up×
Community /Pin to ProfileBookmark

Simple problem passing argument

I’ve got a simple AJAX app and I have two functions for returning information, one in text and the other in XML. Using the code below I’d have to make a function for each ID that receives data. The functions below create the desired output but see the second version to see what I want them to do.

[code]
function createRequestObject()
{
var request_o; //declare the variable to hold the object.
var browser = navigator.appName; //find the browser name
if(browser == “Microsoft Internet Explorer”){
/* Create the object using MSIE’s method */
request_o = new ActiveXObject(“Microsoft.XMLHTTP”);
}else{
/* Create the object using other browser’s method */
request_o = new XMLHttpRequest();
}
return request_o; //return the object
}
var http = createRequestObject();

function getClientList()
{
// opens the portfolio page and requests the client list
http.open (‘get’, ‘portfolio.php?action=client_list&filename=portfolio.xml&listof=client_name&withExpand=1’);
// calls the client list function defined below
http.onreadystatechange = getResultsText;
// sends the data (null is for the get method)
http.send(null);
}

function getResultsText()
{
if (http.readyState == 4) // finished loading the request
{
var response = http.responseText; // assigns the request as text
document.getElementById(‘client_list’).innerHTML = response; // outputs the information to the ‘client_list’ div
}
}
[/code]

The code below gives no output. I highlighted in bold the changes to show what I need the function to do.

I’m not terribly familiar with Javascript except for form validation so this is probably something simple.

[code]
function getClientList()
{
// opens the portfolio page and requests the client list
http.open (‘get’, ‘portfolio.php?action=client_list&filename=portfolio.xml&listof=client_name&withExpand=1’);
// calls the client list function defined below
http.onreadystatechange = getResultsText([B]’client_list'[/B]);
// sends the data (null is for the get method)
http.send(null);
}

function getResultsText([B]byId[/B])
{
if (http.readyState == 4) // finished loading the request
{
var response = http.responseText; // assigns the request as text
document.getElementById([B]byId[/B]).innerHTML = response; // outputs the information to the ‘client_list’ div
}
}
[/code]

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@etylocusOct 30.2006 — Your problem is with the assignment of the onreadystatechange event:

In this case,
[CODE]http.onreadystatechange = getResultsText;[/CODE]
You're assigning to it a function (getResultsText).

In this case,
[CODE]http.onreadystatechange = getResultsText('client_list');[/CODE]
You're assigning to it the results of calling the function (getResultsText) with the value 'client_list'. The workaround of this is not preciselly simple at least in javascript. What i usually do is i send the data about the object in the request (see code in red):

[CODE]
function getClientList([COLOR=Red]objID[/COLOR])
{
http.open ('get', 'portfolio.php?action=client_list&filename=portfolio.xml&listof=client_name&withExpand=1&[COLOR=Red]HTMLID='+objID[/COLOR]);

[/CODE]


And i put that information in the results as well, easyly extractable with string functions. Hope this helps.
Copy linkTweet thisAlerts:
@wallywheelsauthorOct 31.2006 — The first version assigns the function to onreadystatechange as well and it works, the only difference is that there is an argument passed to specify the div id in the second one. That's why I thought this would be a simple problem.

I don't precisely understand how assigning getResultsText to onreadystatechange works anyway, I modeled it after a tutorial. How does the value get assigned if getResultsText doesn't return a value?

Also, for my purposes I need to specify the div ID after I receive information from the PHP script. getResultsText will be used for many functions and in most cases the div ID to be used is specified by the PHP script.

Thanks for your help and anymore advice is greatly appreciated.
Copy linkTweet thisAlerts:
@etylocusOct 31.2006 — Ok, then let's got back to the start.

.onreadystatechange is a callback function, and it's undefined. Is the function that is called when the state of the XMLHTTP object changes. When you do this, [B]xmlhttp.onreadystatechange=my_callback_function;[/B] you're assigning to it a FUNCTION (my_callback_function), so when the state ob the object changes, my_callback_function is called. In your case you're assigning the function [B]getResultText[/B]. When you do this [B]http.onreadystatechange = getResultsText('client_list');[/B] you're assigning to it the results of calling getResultsText with 'client_list', so when the state of the XMLHTTP object changes, nothing is going to happen because .onreadystatechange holds a value and not a function.

I think my previous comment was clear enough as an explanation of what your code is doing, and why it's not working. Besides that, and i quote you:

Also, for my purposes I need to specify the div ID after I receive information from the PHP script. getResultsText will be used for many functions and in most cases the div ID to be used is specified by the PHP script.[/QUOTE]

is solved by this (here i'm quoting myself, from the post previous to mine, man i must be psyquic or something):

You're assigning to it the results of calling the function (getResultsText) with the value 'client_list'. The workaround of this is not preciselly simple at least in javascript. What i usually do is i send the data about the object in the request (see code in red):


[CODE]function getClientList([COLOR=Red]objID[/COLOR])
{ http.open ('get', 'portfolio.php?action=client_list&filename=portfolio.xml&listof=client_name&withExpand=1[COLOR=Red]&HTMLID='+objID[/COLOR]);[/CODE]



And i put that information in the results as well, easyly extractable with string functions. Hope this helps.
[/QUOTE]


So, what do you understand of [B]YOUR[/B] code?
Copy linkTweet thisAlerts:
@wallywheelsauthorOct 31.2006 — Interesting. I didn't realize passing an argument caused the function to be assigned that value. I'll see if I can work this out. Thanks for the help.

EDIT: Extracting the string hasn't been so simple. Could you expand on that last bit?

For example, if the script outputs:

<div id="client_4">

<div class="name">Name</div>

<div class="description">Description</div>

</div>

And I want to send this to the "client_list" div, what is the best way to embed the id and how would you extract it? I've tried making an html tag containing the info and extracting it with a regular expressions search but haven't had much luck.
Copy linkTweet thisAlerts:
@etylocusNov 02.2006 — Hi! Yesterday was a Bank holyday in Spain, so i didn't work (and that means zero internet activity for me).

Now to the problem. Your could change the output so you get this:

[COLOR=Red]client_list|||[/COLOR][COLOR=Blue]<div id="client_4">

<div class="name">Name</div>

<div class="description">Description</div>

</div>[/COLOR]


Then, this code should do the trick:

[CODE]var response=http.responseText.split("|||"); [COLOR=Green]// i used here [COLOR=Blue]|||[/COLOR] as separator, you have
//to use sth that you'll be sure that won't appear anywhere else in the response[/COLOR]
document.getElementById(response[0]).innerHTML=response[1];[/CODE]

I hope this solves your problem.
Copy linkTweet thisAlerts:
@wallywheelsauthorNov 02.2006 — Crikey, that's brilliant. Thanks man.
×

Success!

Help @wallywheels spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...