Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts

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

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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.
    Last edited by AndrewGSW; 10-06-2012 at 06:24 PM.
    "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

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    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>

  • Users who have thanked xelawho for this post:

    Chris-2k (10-06-2012)

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • Users who have thanked AndrewGSW for this post:

    Chris-2k (10-06-2012)

  • #5
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    I dont know, as u see php is generating the random number, so all that needs changing is that number...

  • #6
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • Users who have thanked AndrewGSW for this post:

    Chris-2k (10-06-2012)

  • #7
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Thanks both of ya's, both ideas worked...........


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •