Page 1 of 1

reload php-image

Posted: Tue Jul 18, 2006 2:50 pm
by julian_lp
I've, inside an html page, an image which is created through a php file :


<img src="<?php echo 'captcha.php'; ?>">

file captcha.php, sends
header("Content-type: image/png");
and assign a value to an a session variable

All works fine, but I need to reload the image (a new captcha code should be generated) without reloading the enitre page. Are there any solution? It seems to be a javascript related question...

I'm working on a "post comment" form using ajax.

Posted: Tue Jul 18, 2006 3:00 pm
by daedalus__
Why wouldn't you just say

Code: Select all

<img src="captcha.php" />
?

It's the same as echoing it. You might want to say

Code: Select all

echo '<img src="captcha.php?v='.$random_letter.'" />';
Have it do something a tad bit different every time?

Posted: Tue Jul 18, 2006 3:19 pm
by julian_lp
Daedalus- wrote:Why wouldn't you just say

Code: Select all

<img src="captcha.php" />
?
Yes, of course. I just modified the original code which had a variable there:

<img src="<?php echo $PATH_TO_CAPTCHA; ?>" />

BTW: Do you have any advice in regards to my original question?

Posted: Tue Jul 18, 2006 3:53 pm
by daedalus__
I don't completely understand your original question.

Usually when you use PHP to display an image, you use the GD library but if all that script does is send a image/png header then I don't know what it will do.

Posted: Tue Jul 18, 2006 3:57 pm
by Burrito
take a look here:

viewtopic.php?t=49466

Posted: Tue Jul 18, 2006 6:45 pm
by julian_lp
Burrito wrote:take a look here:

viewtopic.php?t=49466


Great thread. Sadly, even though I've read it, I cant get the job done. While I re-read that post entirely, could someone give me any hint?


This is how I generate, when the page loads, the captcha image (works well)

Code: Select all

<img id="mainimage" src="simplecaptcha/captcha.php" alt="CAPTCHA">
This is the link to update the captcha image, without reloading the page. The function ReloadCaptcha is already called, but no new captcha image is loaded, and I don't understand why...
Note that I tried both with absolute and relative paths to the php file

Code: Select all

<a href="javascript:void(0)" onclick="ReloadCaptcha();">give me an easier one!</a>
		
		
		<script type="text/javascript">
		
			function ReloadCaptcha(){
				alert ('ReloadCaptcha');

				///document.getElementById('mainimage').src='simplecaptcha/captcha.php'; 
				document.getElementById('mainimage').src='http://REALPATH/captcha.php'; 				
				return false;
			}
              </script>

Posted: Tue Jul 18, 2006 7:08 pm
by Weirdan
Try

Code: Select all

document.getElementById('mainimage').src='http://REALPATH/captcha.php?' + Math.random();
It's an ages old trick used to force picture to be re-requested from server.

Posted: Tue Jul 18, 2006 7:13 pm
by julian_lp
Weirdan wrote:Try

Code: Select all

document.getElementById('mainimage').src='http://REALPATH/captcha.php?' + Math.random();
It's an ages old trick used to force picture to be re-requested from server.

Let me tell you something in one word:
It_works_like_a_charm :P :P :P :P

I'd like to know why though :roll:

many thanks Weirdan

Posted: Tue Jul 18, 2006 7:24 pm
by Weirdan
I'd like to know why though
Because every time it creates new URL which browser has not seen yet, so it dutifully fetches the resource from the server.

Posted: Tue Jul 18, 2006 8:06 pm
by julian_lp
Weirdan wrote:
I'd like to know why though
Because every time it creates new URL which browser has not seen yet, so it dutifully fetches the resource from the server.
ahhhh, so it's all about browser's behavior, nothing related to php nor javascript. Interesting...

Posted: Wed Jul 19, 2006 11:09 am
by Ward
Lookup the javascript setInterval function. Using this, you can have a function run every second or so, which would simply refresh the image.

Posted: Thu Jul 20, 2006 4:32 am
by Weirdan
Lookup the javascript setInterval function. Using this, you can have a function run every second or so, which would simply refresh the image.
It's weird idea to change captcha every second...