←  Code Block

Fallout Studios Forums

»

[AJAX] Assigning variables

Slightly Wonky Robob's Photo Slightly Wonky Robob 14 May 2008

Is it possible to retrieve data via AJAX and instead of putting that data into a html object, put it straight into a variable for future processing?
Quote

CodeCat's Photo CodeCat 15 May 2008

I don't think so. AJAX uses the XMLHttpRequest object, which sends and receives only XML data. It's up to the programmer to interpret the data as they see fit.
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 15 May 2008

It just seems stupid to me, since you can retrieve the data, put it in a hidden div, then split it up and put it into variable... so I just can't understand why you can't just cut out the middle man :)
Quote

CodeCat's Photo CodeCat 15 May 2008

A hidden div? I'm not really sure I understand what you mean...
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 15 May 2008

a div, with visibility set to hidden >.>
Quote

CodeCat's Photo CodeCat 16 May 2008

Yes, but why do you need one? Can't you just do it all in JavaScript?
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 16 May 2008

no, that's the problem..

Here's the current situation: I have a mini slideshow which is on the main page of my gallery. Initially a random image is loaded with an onload parameter calling a function. This function in turn does a series of things; A time is started. It picks a random number between 1 and the highest id in the database (this is stored in a variable which is created by php on the initial load). The function then uses the number on a simple preload function, that loads the image in the background, so that there is no lag. Once the timer has run out (and providing the mouse isn't over the image), the image fades out, the image is swapped with the new (with the same onload parameter) and finally the new image fades in.

Now this is all fine, it all works great... apart from a small error that occurs every now again, this is because some images in the database have been deleted, meaning when the new image is swapped in it's just blank. Now this is bad in its self, because it doesn't look very good, but on top of that it causes the slideshow to crash, because the onload function cannot be triggered.

This is where AJAX comes in, I want to call a php page that will pick a random image (that exists) and while I'm at it, return other information such as the title and ratings. The problem with this is that I can't call on the function, store the values in an array, preload the image, then when timer runs out the rest of the proccess continues as normal
Quote

CodeCat's Photo CodeCat 16 May 2008

Couldn't you try a different approach, by first getting a list of all possible images from the database, and then generating a random one of those? That would avoid accidentally choosing an image that doesn't exist.
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 16 May 2008

Yes I could, but I'm not talking about just a few images, I'm talking about 500+ images, that would a complete waste if a full list had to be accessed and downloaded each time the homepage is visited

EDIT: I think I may have a solution, just a theory at the moment...

- The timer is started
- A random image is chosen from the php file, along with the info
- Image put in preload function
- info stored in hidden div
- timer runs out
- image fades/swapped
- info fades/swaps

Just seems stupid having to put the info in the hidden div, but there's not really another way...

EDIT2: works perfectly :P
Edited by Bob, 16 May 2008 - 19:43.
Quote

CodeCat's Photo CodeCat 16 May 2008

Why put the info in a hidden div when it can go in a JavaScript variable?
Quote

Slightly Wonky Robob's Photo Slightly Wonky Robob 16 May 2008

View PostBob, on 14 May 2008, 21:16, said:

Is it possible to retrieve data via AJAX and instead of putting that data into a html object, put it straight into a variable for future processing?


>.>

Every time I've tried it just doesn't work :S
Quote