我以这种方式显示验证码:
<img src="ReturnCaptcha2.php" alt="CAPTCHA" width="100" height="50">
我在它附近有一个名为“重新加载”的超链接。我想在不刷新页面的情况下调用那个 PHP 文件。可能,AJAX 参与其中。请协助。
我以这种方式显示验证码:
<img src="ReturnCaptcha2.php" alt="CAPTCHA" width="100" height="50">
我在它附近有一个名为“重新加载”的超链接。我想在不刷新页面的情况下调用那个 PHP 文件。可能,AJAX 参与其中。请协助。
是不是有一些带有时间戳之类的参数,所以请求不会被缓存?为什么不将 src 替换为(使用 jquery)
$("img#your-selector").attr("src","ReturnCaptcha2.php?_="+((new Date()).getTime()));
<a href="javascript:;" title="Reload Image"
onclick="document.getElementById('captcha').src = 'cimage.php?' + Math.random();
return false">
<img src="cimage.php" alt="Enter captcha" title="Enter captcha" id="captcha"/>
</a>
这是我与 Secureimage PHP Captcha 一起使用的代码。
<form method="POST">
<input type="hidden" name="PHPSESSID" value="0b21dde61d891cd" />
<img id="siimage" src="securimage_show.php?sid=2ef186788e" alt="CAPTCHA Image" width="126" height="36" /><br />
<input type="text" name="code" /><br />
<a href="#" onclick="document.getElementById('siimage').src = 'securimage_show.php?' + Math.random(); return false">Reload Image</a>
<input type="submit" value="Submit Form" />
</form>
为什么不使用http://recaptcha.net/并为自己省去这一切的麻烦?他们只会为您处理验证码。
以下是关于 PHP 和 recaptcha 的一些集成信息: http ://www.google.com/recaptcha/plugins/php
正如我所提到的,集成到您的 web 应用程序中一点也不难。试试看!
也许是这样的:
function reloadCaptcha()
{
img = document.getElementById('captcha');
img.src = 'ReturnCaptcha2.php';
}
然后添加一个带有 的按钮onclick="reloadCaptcha();"
。
我还没有测试过,我也不是 js 大师,所以不确定它是否有效。
另一种方法可以使用一点 ajax .. 在您的刷新按钮上将操作设置为:onclick=javascript:reloadCaptcha();
在这个 reloadCaptcha 函数中,调用您的 captcha.php 并使用返回值更新保存您的验证码的 div。您可以轻松地返回一个 html,然后将其嵌入到 div 中。
这是另一个。
它创建全局变量。检查是否存在。
function get_captcha(){
//get original url refresh
console.log('captcha requested.');
if(typeof window.captcha_src === 'undefined'){
window.captcha_src = document.getElementById('captcha').src;
}
document.getElementById('captcha').src = window.captcha_src + '&nocache=' + Math.random();
}
您甚至可以在同一部门创建链接并重新加载您的验证码。如果不是问题。
Enter Image Text <input name="captcha" type="text">
<img src="captcha.php" /><br>
<a href="captcha.php">Reload Captcha</a><br/>
好吧,我用一个简单的技巧解决了这个问题;而不是将验证码<img>
放入其中<iframe>
并在您需要时使用javascript重新加载它
Javascript 重新加载:
function reloadCaptcha()
{
document.getElementById('cap').src='captcha.php';
}
实际 HTML:
<iframe id="cap" src="captcha.php"></iframe>
<input type="button" value="reload" onclick="reloadCaptcha();"/>
它解决了我的问题,如果我在某处错了,请纠正我
@meme: 我使用相同的东西,但使用 jquery。如果您在一页中需要几个 capthcas:
<img class="captcha" src="/securimage/securimage_show.php" alt="CAPTCHA Image">
<a class="capcha-refresh" href="#" ><img src="/images/Refresh-icon.png"></a>
和js函数:
$('.capcha-refresh').click(function(a){
$('.captcha').attr('src','/securimage/securimage_show.php?' + Math.random());
a.preventDefault();
})