views:

88

answers:

3

Hello!

This question may have been asked already - but unfortunately, I could not find any satisfactory answers. I will just ask it for my concrete case and ask the admins not to delete the question for at least a few days so I can try it out...

I have a page. It uses a captcha. Like so:

<?php
session_start(); // the captcha saves the md5 into the session
?>
<img src="captcha.php" onclick="this.src = this.src" />

That was my first code. It did not work, because the browser condsidered it useless to reload an image if the source is the same. My current solution is to pass a get parameter:

onclick="this.src = 'captcha.php?randomNumber='+ranNum"

The JavaScript variable var ranNum is generated randomly every time the onclick event fires. It works fine, still, I don't like the possibility, if the - though improbable - case of two numbers being the same twice in a row. Although the random number varies between -50,000 and 50,000 - I still do not like it. And I don't think the method is right. I would like to know the 'righter' method, by means of AJAX. I know it's possible. I hope you know how it's possible ^^ In that case, please show me.

Thanks in advance!

By the way - if I spell cap(t)cha differently, never mind, the reference to the PHP file is right in my code: I use randomImage.php

EDIT: The random number in JavaScript is only generated so the image reloads. Captcha.php does not care for the $_GET parameter. The string really is random.

EDIT: Thus, what I would like to know is how to make the browser relaod the image without passing different get parameters every time the event fires.

+3  A: 

Have you considered using a timestamp instead?

onclick="this.src='captcha.php?ts='+Math.round(new Date().getTime()/1000)"
karim79
that sounds interesting. there would be no risk of a repetitive number. still, I would like to know how it's done with AJAX ^^
arik-so
That's asynchronous and makes use of JS, hence it's AJAX.
nico
@Nico: there's no XML and it doesn't make use of XMLHttpRequest, hence it's not technically AJAX. The browser is just loading the image for you when it detects the new source.
Eric Mickelsen
You can call up the server to send the new image source, which would be a server generated timestamp, e.g. `onclick="captcha.php?ts=<?php echo time() ?>"`. However, I don't see any real benefit to doing it that way, it seems like another way of doing the same thing.
karim79
that would not work. `time()` would be generated once beforehand, but later, it would be the same, thus, the source would not change and the image would not reload.
arik-so
@arik-so, you misunderstood me. What I meant was, onclick the client sends a XHR request and fetches the new source, which is either the same thing with a timestamp appended or a new source filename altogether, which is what I had meant by "call up the server". I should have been clearer...
karim79
@tehMick: well... that's really nitpicking ;)OK, it's not technically AJAX but it is asynchronous loading of an image. You could make use of an XMLHttpRequest to do it, of course, but it really wouldn't do any good at all.PS: I disagree on the fact that AJAX requires XML, that's just in the name, but very often other type of responses are used. For instance I like to have my AJAX calls return JSON, which I find way easier to handle in JS then XML.
nico
@nico - You'd actually have a hard time using XMLHttpRequest to actually load an image - it's probably possible, but way overkill for the OP. You're right, lots of "AJAX" uses JSON instead, but changing image sources with javascript precedes the advent of AJAX by several years, so I don't think it qualifies.
Eric Mickelsen
@tehMick: no, no I wasn't suggesting to use XMLHttpRequest (although I guess it could be technically feaseable)!!!The fact that reloading sources was possible even before AJAX was invented doesn't make it less asynchronous :) He's loading an image from the server AFTER the page has been loaded, that's it.Consider that XMLHttpRequest dates back to 2000, 5 years before the term AJAX was even invented (2005)!!! So technically AJAX was possibly before the term was even thought of! :D
nico
+1  A: 

Just use:

<img src="captcha.php" onclick='this.src = "captcha.php&time=" + new Date().getTime();' />

You can discard the time parameter and generate the random number in PHP. :)

nico
thanks. I'm sorry, the question was not clear, I DO disregard the get parameter. It's only used to force the browser relaod the picture ;) Still, I would like to know how I can force the browser to realod the picture WITHOUT passing a random get parameter.
arik-so
@arik-so As far as I know there's no way to do that. If the browser sees the image's src is the same it won't reload it.I looked into a similar problem recently and that's pretty much the only solution I found. Of course, if someone knows of another option I would be very glad to know it!
nico
well, I will wait and leave the question open. Maybe somebody will find an answer.
arik-so
+4  A: 

Unfortunately, AJAX doesn't provide a good way to dynamically load images. Even using the javascript "preload" trick just gets the browser to load each image once per URL. The only good way to get the browser to load another image from the same source is to use a different parameter just like you are doing now. And, like other answers have stated, timestamp should be sufficient for that.

Eric Mickelsen
Well... that's quite unfortunate. Thanks.
arik-so