5

I am using simple captcha in my JSP. Everything is OK. I want to provide a refresh button alongside of captcha to allow user to change the captcha. Captcha changes only on refreshing the complete page but I don't want to reload whole page for it.

I need your suggestions on how I can implement this like using AJAX or JQuery to reload only the captcha, not whole page.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sandeep Kumar
  • 13,799
  • 21
  • 74
  • 110

3 Answers3

3
<script type="text/javascript">
    function reloadCaptcha(){
        var d = new Date();
        $("#captcha_image").attr("src", "/captcha_generator.jsp?"+d.getTime());
    }
</script>
   ...
<img id="captcha_image" src="/captcha_generator.jsp" alt="captcha image" width="200" height="50"/>
<img src="reload.jpg" onclick="reloadCaptcha()" alt="reload"width="40" height="40"/>
sachleen
  • 30,730
  • 8
  • 78
  • 73
javaghost
  • 46
  • 3
0

I don't remember how SimpleCaptcha works, but usually, you should simply change the 'src' attribute of you captcha <img>. Something like this on the onClick of your refresh button:

var img = document.getElementById('captcha_id');// captcha_id is the id attribute of your caprtcha img
img.src = 'Some new captcha url';
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
0
<script type="text/javascript">
    function reloadCaptcha(){
        var d = new Date();
        $("#captcha_image").attr("src", "captcha.php?"+d.getTime());
    } </script>

<img id="captcha_image" src="captcha.php" alt="captcha image" width="90" height="33"/> <img src="images/refresh.png" alt="reload" width="22" height="22" border="0" onclick="reloadCaptcha()" />