/    Sign up×
Community /Pin to ProfileBookmark

Close to losing it!

Hello everyone,

About a week ago I started learning ajax and also tried to improve some javascript skills. I done this to try and create something for a website that I am doing in work for a client. It was going well but I’ve hit a brick wall now, and can’t figure it out.

I have a select box that calls a function. This function then goes to a php page where a query was being created. At first I was just doing this echo “<optoin…..”> but now I want to return the query information so I can build the objects by DOM.

How can I do this?

JSON was suggested, but I just can’t figure out how to use it. The documentation seems very confusing and ambigious. Is it hard to use XML?

I quite like the idea of using XML. I have to send the data in a good/suitable format and then use XML to read it.

Any suggestiosn on how to do this or at least a starting point?

I’m going mad at the minute and spent over a week trying to solve this.

Thanks,
Gary.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@crushmeguySep 15.2006 — The following is a very basic example of passing values to and from the server without using XML.

Let's say that I want to send two numbers to my server. I want the server to add these numbers together, and return the sum. I realize this math could be done very easily with javascript, but for the sake of the example we will not do that.

My xmlhttprequest's POST value is simply an ampersand delimited string that contains the two values I want the server to add. For example, I want to add the numbers 3 and 5. My POST string is as follows: "3&5".

On the server I parse the values 3 and 5 and repackage their sum. I also send a 'success' code so that my javascript knows there were no errors on the server. Again, my response stream is simply an ampersand delimited string. For example, "success&8". Thats the only thing I send back to my xmlhttprequest object.

My javascript looks at the value of xmlhttprequet.responseText (which is "success&8"). It breaks the string up into is seperate componants ('success' and '8').

Then I simply create a new DOM element, lets say a div, and populate is as I desire. Finally I place this element into whatever parent it should belong to.

Here is some psuedo code (assuming you are familiar with creating your xmlhttprequest object):

[CODE]
var result = [XMLHttpRequestObject].responseText;
var resultArray = result.split('&');
if(resultArray[0] == 'success'){
var newDiv = document.createElement('div');
newDiv.innerHTML = resultArray[1];
[ParentElement].addChild(newDiv);
}
[/CODE]


The short of it is, you don't really need the server to send back valid XML to get information to the client. In complex applications XML sure does help, but not in a simple case like this.

I hope that cleared something up for you.

Doug
×

Success!

Help @redrabbit 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 4.28,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...