reload php-image

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

reload php-image

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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?
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

take a look here:

viewtopic.php?t=49466
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
User avatar
julian_lp
Forum Contributor
Posts: 121
Joined: Sun Jul 09, 2006 1:00 am
Location: la plata - argentina

Post 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...
Ward
Forum Commoner
Posts: 74
Joined: Thu Jul 13, 2006 10:01 am

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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...
Post Reply