我申请了zend验证码在我的php页面,现在我需要添加验证码重新加载按钮。请根据zend给出答案。
发布于 2011-05-05 18:34:56
只有两个简短的片段,但我想你会明白的。根据需要调整元素名称和选择器。
在您的控制器中添加一个方法来生成一个新的验证码
public function refreshAction()
{
$form = new Form_Contact();
$captcha = $form->getElement('captcha')->getCaptcha();
$data = array();
$data['id'] = $captcha->generate();
$data['src'] = $captcha->getImgUrl() .
$captcha->getId() .
$captcha->getSuffix();
$this->_helper->json($data);
}
在视图脚本中(我对ajax-request使用mootools )
document.addEvent('domready', function() {
$$('#contactForm img').addEvent('click', function() {
var jsonRequest = new Request.JSON({
url: "<?= $this->url(array('controller' => 'contact', 'action' => 'refresh'), 'default', false) ?>",
onSuccess: function(captcha) {
$('captcha-id').set('value', captcha.id);
$$('#contactForm img').set('src', captcha.src);
}
}).get();
});
});
编辑:添加pahan的jquery
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
$.ajax({
url: '/contact/refresh',
dataType:'json',
success: function(data) {
$('#contactForm img').attr('src', data.src);
$('#captcha-id').attr('value', data.id);
}
});
});
});
发布于 2011-06-29 02:36:17
@user236501实际上它可以是任何类型的Zend表单元素(例如Button)。您甚至可以将可点击的刷新链接作为Zend_Form_Element_Captcha描述选项,如下所示:
$captcha = new Zend_Form_Element_Captcha('captcha', array(
'label' => 'Some text...',
'captcha' => array(
'captcha' => 'Image',
'wordLen' => 6,
'timeout' => 300,
'font' => './fonts/Arial.ttf',
'imgDir' => './captcha/',
'imgUrl' => 'http://some_host/captcha/'
),
'description' => '<div id="refreshcaptcha">Refresh Captcha Image</div>'
));
但在这种情况下,应该修改Description
装饰器的选项,例如:
$this->getElement('captcha')->getDecorator('Description')->setOptions(array(
'escape' => false,
'style' => 'cursor: pointer; color: #ED1C24',
'tag' => 'div'
));
这可以在form的init()
方法中完成。对不起,我的英语不好。顺便说一句,我不确定我是否把我的评论放在了正确的地方;)
发布于 2011-06-08 19:04:01
@本杰明·克雷默感谢你的代码,就像魅力一样:)读完这篇文章后,我用jquery完成了这项工作。
$(document).ready(function() {
$('#refreshcaptcha').click(function() {
$.ajax({
url: '/contact/refresh',
dataType:'json',
success: function(data) {
$('#contactForm img').attr('src',data.src);
$('#captcha-id').attr('value',data.id);
}
});
});
});
https://stackoverflow.com/questions/5741753
复制相似问题