I have a form with captcha validation that looks like this:
The code of the captcha inside the html form is the following:
<div class="form_title">Enter your captcha code: </div>
<input name="captcha_code" id="captcha-input" type="text" class="demo-input captcha-input" style="height:40px;">
the css calls the php file which generates the code in the form of an image like this
.captcha-input {
background: #FFF url(./../captcha/captchaImageSource.php) repeat-y left center;
padding-left: 80px;
}
.demo-input {
width: 100%;
border-radius: 5px;
border: #CCC 1px solid;
}
The captchaImageSource.php generates the image and looks like this
<?php
namespace abc;
use abc\Captcha;
session_start();
include("captcha.php");
$captcha = new Captcha();
$captcha_code = $captcha->getCaptchaCode(6);
$captcha->setSession('captcha_code', $captcha_code);
$imageData = $captcha->createCaptchaImage($captcha_code);
$captcha->renderCaptchaImage($imageData);
?>
I am trying to reload the captcha with a button, e.g if the user cannot read it and wants to try another. I have tried multiple approaches like, calling a javascript or jquery function which triggers the captchaimagesource again like this with javascript
<input type="button" onclick="reloadCaptcha();" id='reload_captcha_button'/>
<script>
function reloadCaptcha(){
document.getElementById("captcha-input").onclick = location.reload;
document.getElementsByClassName("captcha-input").onclick
}
</script>
or like this with jquery:
<a id='reload_captcha_button' href='#'>Refresh captcha image</a>
<script>
$('#reload_captcha_button').click(function(){
var timestamp = new Date().getTime();
$('#captcha-input').attr('src','captcha/captchaImageSource.php?'+timestamp)
})
</script>
I also tried with ajax but the image stays the same. Any ideas how to update the captcha in the same form? Thanks