用PHP形式刷新Captcha

时间:2012-06-05 10:29:28

标签: php

我正在尝试将Securimage PHP脚本(cf google)用于CAPTCHA,但我遇到了一个问题:当我按下“不同图像”链接(显示另一个验证码)时,我丢失了输入的所有文本我的PHP表单的字段。因此,用户必须再次重新输入所有内容,这是有问题的。

这是代码。 注意:我为href带走了'#',因为由于某种原因,它无法更改验证码图像。有谁知道为什么? (这在Safari和Firefox中都会发生)

<img id='captcha' src='/myproject/securimage/securimage_show.php' alt='CAPTCHA Image' /><br> <a href='' onclick='document.getElementById('captcha').src =  '/securimage/securimage_show.php?' + Math.random(); return false'> [Different Image ]</a>

2 个答案:

答案 0 :(得分:1)

因为您已从href中删除了“#”,这就是为什么在单击链接并且所有字段数据都丢失时刷新整个页面的原因。不要删除它,然后使用以下代码

<img id='captcha' src='/myproject/securimage/securimage_show.php' alt='CAPTCHA Image' />
<br> 
<a href="#" onclick="document.getElementById('captcha').src =  '/securimage/securimage_show.php?' + Math.random(); return false;"> [Different Image ]</a>

答案 1 :(得分:1)

这不是因为删除了'#'。页面重新加载,因为您在<a>标记中使用了错误的引号。对html属性使用单引号'以及在onclick中包含javascript时,浏览器只会使用document.getElementById(作为onclick事件。 <{1}}将不会被执行,页面会重新加载。

您可以始终将return false;用于html属性,将"用于javascript。或者使用'来转义attrbiutes内的相同引号。

这也可以没有'#':

\

或者它的转义版本:

<a href="" onclick="document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); return false;"> [Different Image ]</a>