Page 1 of 1

AjaxMadeSimple - onClick event

Posted: Sun Nov 18, 2007 1:50 pm
by JayDee
Hi guys,

On my website i have a banner which now changes as you reload the page.
I would like a add a button 'Shuffle' and make a random image appear, via Ajax. I have started this imagemodule and made something working. However, when i click the 'shuffle' button, i'm not getting 1 new image, the image keep changing untill i press esc.

How my module looks like now: (btw, 'foto' is dutch for 'picture')

BannerFoto.module.php
  • Containts a function init()
    Checks for pictures in a directory and generates an (shuffled) array in $_SESSION['fotos']
  • Contains a function MaakFoto()
    Picks a random image from session and echoes html:
action.default.php
  • Checks whether $_SESSION['fotos'] is set. If not run $this->init()
  • Run $this->MaakFoto();, this displays the photo on page acces
  • Register Ajax requester:
    $ajax->RegisterAjaxRequester($this->GetName(), 'bnnr','fpbanner','MaakFoto',false,array(), array(),-1);
From a the session(['ajaxmsgeneratedcode']) i found the javascript function makeBannerFotosbnnrRequest().

I call these functions from a content page, using

Code: Select all

<div id="fpbanner">
{cms_module module="BannerFotos"}
</div>
<span onClick="return makeBannerFotosbnnrRequest();">Klik!
</span>
Unfortunately clicking the shufflelink does not replace the content of my div.fpbanner once, it keeps picking a random image (function MaakFoto)

Code: Select all

function makeBannerFotosbnnrRequest() {
  var http_request=gethttp();
  http_request.onreadystatechange = function() { alertBannerFotosbnnr(http_request,true); };
  http_request.open('GET', 'http://www.nsrvphocas.nl/modules/AjaxMadeSimple/requesthandler.php?module=BannerFotos&method=MaakFoto&', true);
  http_request.send(null);
}
Does anyone have some experience on this subject?

Re: AjaxMadeSimple - onClick event

Posted: Mon Nov 19, 2007 7:08 pm
by Silmarillion
Hi JayDee

I take it you did a form with a submit button which includes the ajax-code, something like:
GetFormOnSubmit($this->GetName(),"bnnr").'>


Could I see your whole module? I am very interested as I actually considered doing some kid of AjaxMS-driven gallery myself, but never got around to it...

Regards
Morten/Silmarillion

Re: AjaxMadeSimple - onClick event

Posted: Mon Nov 19, 2007 9:31 pm
by JayDee
No, i don't want my page to have a submit button. I want a onclick event on a <a, <span or <div (or any html object of choice).

From the $_SESSION variable i found a Javascript function makeBannerFotosbnnrRequest(); I tought using this as an onclick event would do the trick. It does, but it keeps doing it...

I'll attach the XML of my module so far. There is no admin function so you'll have to set the image directory in the source.
Create a content page containing the following code (enter via HTML source view)

Code: Select all

<!-- This is the image -->
<div id="fpbanner">{cms_module module="BannerFotos"</div>

<!-- This is the button to change the picture -->
<span onmousedown="return makeBannerFotosbnnrRequest();">Klik!</span>

<!-- Echos the Javascript, could not found out why the continues updates happen though -->
<pre>{php}echo $_SESSION['ajaxmsgeneratedcode'];{/php}</pre>
And a example: http://www.nsrvphocas.nl/?page=testing