简体   繁体   中英

jQuery / php refresh CAPTCHA

I'm using a simple captcha in my contact us page. I would like to add a refresh function using javascript / jQuery. The function that makes the captcha image is in a separate file. When the page loads the CAPTCHA works well but I can't make the refresh. My code is:

<head>
...
    $(document).ready(function(){
        $('#reload').click(function(){
            $("#captcha").load("functions/captcha.php"); // function that creates the NEW CAPTCHA
            $("#captcha").attr("src", "images/captcha.jpg?"+(new Date()).getTime());
        });
    });

...
</head>
<body>
<?php 
  require(functions/captcha.php);
 ?>

  // form
  ...
  <label for="captcha_code">ENTER CAPTCHA</label>
    <img src="images/captcha.jpg" id="captcha" /> 
      <a id='reload'>Refresh now</a>
        <input type="text" name="captcha_code" value="captcha_code">
  ...

I really would appreciate your help. Thank you for reading.

You should wait before "functions/captcha.php" fully loads.

Try this example:

<div id="loader"></div> <!-- hide this div by css -->
$('#reload').click(function() {
     $("#loader").load("functions/captcha.php",  function() {
          $("#captcha").attr("src", "images/captcha.jpg?refr="+(new Date()).getTime());
     }); 
});

But isn't it better to create functions/captcha.php that outputs an image and just refresh the src parameter?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM