4,250
社区成员
发帖
与我相关
我的任务
分享
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;