Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-06-2012, 06:08 PM   PM User | #1
Chris-2k
Regular Coder

 
Join Date: May 2011
Posts: 303
Thanks: 29
Thanked 0 Times in 0 Posts
Chris-2k is an unknown quantity at this point
Jquery refresh <img>

hi

i have an unusual captcha system and want to refresh the <img src="">

PHP Code:
<input type='button' id='refresh'>
<img src="<?php echo URL_THEME.'images/CaptchaCodes/CaptchaIMG_'.mt_rand(0,20).'.jpg'?>" style="margin-bottom:5px;" title="Captcha Code!" />
im guessing js can do this?

any help.......
Chris-2k is offline   Reply With Quote
Old 10-06-2012, 06:22 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
It will be easier if you give your img an id, then use JS to change the src:

Code:
document.getElementById('imgid').src = "whatever.jpg";
Use JS's version of a random number, something like

Code:
var randomnumber=Math.floor(Math.random()*21);
If the value of URL_THEME is not available to JS somewhere then you'll need to extract it from the existing img's src using indexOf(), substr() (or substring()).

If this describes what you are after then you could report back and I (or someone else ) might help you assemble the code you need.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 10-06-2012 at 06:24 PM..
AndrewGSW is offline   Reply With Quote
Old 10-06-2012, 06:22 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
something like this, if I understand you image paths correctly...
Code:
<input type='button' onclick="changeCap()" id='refresh'> 
<img src="<?php echo URL_THEME.'images/CaptchaCodes/CaptchaIMG_'.mt_rand(0,20).'.jpg'; ?>" id="captcha" style="margin-bottom:5px;" title="Captcha Code!" />
<script>
function changeCap() {
var num=Math.floor(Math.random()*21)
document.getElementById("captcha").src='images/CaptchaCodes/CaptchaIMG_'+num+'.jpg';
        }		
</script>
xelawho is online now   Reply With Quote
Users who have thanked xelawho for this post:
Chris-2k (10-06-2012)
Old 10-06-2012, 06:29 PM   PM User | #4
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Actually, you don't need to extract URL_THEME separately; you just need to replace..

Code:
...CaptchaIMG_8.jpg // with
...CaptchaIMG_yournumber.jpg
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
Chris-2k (10-06-2012)
Old 10-06-2012, 06:54 PM   PM User | #5
Chris-2k
Regular Coder

 
Join Date: May 2011
Posts: 303
Thanks: 29
Thanked 0 Times in 0 Posts
Chris-2k is an unknown quantity at this point
I dont know, as u see php is generating the random number, so all that needs changing is that number...
Chris-2k is offline   Reply With Quote
Old 10-06-2012, 07:06 PM   PM User | #6
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
If URL_THEME won't contain any numbers then modifying xelawho's code:

Code:
function changeCap() {
    var num=Math.floor(Math.random()*21)
    document.getElementById("captcha").src = document.getElementById("captcha").src.replace(/[0-9]+/,num);
}
would just replace the random number with the one JS generates.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
Chris-2k (10-06-2012)
Old 10-06-2012, 07:19 PM   PM User | #7
Chris-2k
Regular Coder

 
Join Date: May 2011
Posts: 303
Thanks: 29
Thanked 0 Times in 0 Posts
Chris-2k is an unknown quantity at this point
Thanks both of ya's, both ideas worked...........
Chris-2k is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:12 AM.


Advertisement
Log in to turn off these ads.