Jump to content


[AJAX] Assigning variables


10 replies to this topic

#1 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 14 May 2008 - 20:16

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?
Posted Image
F O R T H E N S
Posted Image

#2 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 15 May 2008 - 10:28

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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#3 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 15 May 2008 - 18:52

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 :)
Posted Image
F O R T H E N S
Posted Image

#4 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 15 May 2008 - 21:22

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

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#5 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 15 May 2008 - 22:18

a div, with visibility set to hidden >.>
Posted Image
F O R T H E N S
Posted Image

#6 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 16 May 2008 - 00:20

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

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#7 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 16 May 2008 - 09:20

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
Posted Image
F O R T H E N S
Posted Image

#8 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 16 May 2008 - 16:20

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.
CodeCat

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#9 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 16 May 2008 - 17:08

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.

Posted Image
F O R T H E N S
Posted Image

#10 CodeCat

    It's a trap!

  • Gold Member
  • 6111 posts

Posted 16 May 2008 - 22:36

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

Posted Image
Posted Image

Go dtiomsaítear do chód gan earráidí, is go gcríochnaítear do chláir go réidh. -Old Irish proverb

#11 Slightly Wonky Robob

    Not a Wonky Gent.

  • Administrator
  • 9333 posts

Posted 16 May 2008 - 22:54

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
Posted Image
F O R T H E N S
Posted Image



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users