请教一个Zend Framework中ajax验证码的问题

zxywd 2011-03-10 01:40:21
各位下午好。我遇到一个很头疼的问题希望大家帮忙解决一下。是关于Zend Framework中ajax验证码的问题。代码如下:
首先,我修改了library/Zend/Captcha/Image.php文件中的render方法:

public function render(Zend_View_Interface $view = null, $element = null)
{
return '<img id="changeCaptcha" alt="'.$this->getImgAlt().'" src="' . $this->getImgUrl() . $this->getId() . $this->getSuffix() . '" style="cursor: pointer;" /><br/>';
}

IndexController.php

<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
$form = new Zend_Form();
$captcha = $this->createCaptcha();
$form->addElement($captcha);
$this->view->captchaImageUrl = $captcha->getCaptcha()->getImgUrl()
. $captcha->getCaptcha()->getId()
. $captcha->getCaptcha()->getSuffix();

$this->view->formLogin = $form->render();
}

public function ajaxAction()
{
$captcha = $this->createCaptcha()->getCaptcha();
$captcha->generate();
$this->_helper->viewRenderer->setNoRender(true);
echo $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix();
die;
}

public function createCaptcha()
{
$decorators = array(
array('HtmlTag', array('tag' => 'div',
'id' => 'captchaId',
'onclick' => 'changeImage()'))
);

$form = new Zend_Form();

$captcha = $form->createElement('captcha', 'captcha', array(
'captcha' => array(
'captcha' => 'Image',
'wordLen' => 6,
'fontsize' => 20,
'width' => 200,
'height' => 100,
'dotNoiseLevel' => 2,
'timeout' => 300,
'imgUrl'=>'/images',
'imgDir'=>APPLICATION_PATH.'/../public/images',
'font'=>APPLICATION_PATH.'/../public/images/LiberationSansRegular.ttf'
),
'decorators' => $decorators
));

return $captcha;
}
}

index.phtml

<script type="text/javascript">
function createXmlHttpObject()
{
try
{
xmlhttp = new XMLHttpRequest();
} catch (trymicrosoft) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (othermicrosoft) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
xmlhttp = false;
}
}
}
if (!xmlhttp){
alert("无法创建 XMLHttpRequest 对象!");
}
return xmlhttp;
}

var xmlhttp = createXmlHttpObject();
//JavaScript Document
function $(id){
return document.getElementById(id);
}

function changeImage()
{
if(document.activeElement.id=='captcha-input')
{
return false;
}
url="/index/ajax";
xmlhttp.open('GET',url,true);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200)
{
response=xmlhttp.reponseText;
$('changeCaptcha').src=response;
}
}
};
}
</script>


<?php
echo $this->formLogin;


当我用鼠标单击图片区域时,应该会显示新的验证码,不知道为什么,就是不能实现动态验证码的效果。希望各位能帮忙解决一下谢谢了。
...全文
138 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxywd 2011-03-10
  • 打赏
  • 举报
回复
library/Zend/Captcha/Image.php文件中的render方法:刚才没显示清楚,现在重新写一下。

public function render(Zend_View_Interface $view = null, $element = null)
{
return '<img id="changeCaptcha" alt="'.
$this->getImgAlt().'" src="' . $this->getImgUrl() .
$this->getId() . $this->getSuffix() .
'" style="cursor: pointer;" /><br/>';
}

4,250

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧