Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload Zend Captcha image on click refresh button?

I apply a zend captcha in my php page now i require to add captcha reload button. Please give answer according to zend.

like image 892
Rajesh purohit Avatar asked Apr 21 '11 08:04

Rajesh purohit


3 Answers

Just two quick snippets but I think you will get the idea. Adjust the element name and the selectors for your needs.

In your controller add a method to generate a fresh captcha

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);
}

In your view script (I'm using mootools for the ajax-request)

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();
    });
});

Edit: Added pahan's 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); 
            }
        }); 
    }); 
});
like image 80
Benjamin Cremer Avatar answered Nov 06 '22 10:11

Benjamin Cremer


@user236501 Actually it can be any type of Zend Form Element (for example Button). You're even able to put clickable refresh link as Zend_Form_Element_Captcha description option like this:

        $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>'
        ));

but in that case Description decorator's options should be modified, for example:

        $this->getElement('captcha')->getDecorator('Description')->setOptions(array(
            'escape'        => false,
            'style'         => 'cursor: pointer; color: #ED1C24',
            'tag'           => 'div'
        ));

It can be done in form's init() method. Sorry for my english. Btw I'm not sure if I put my comment in the right place ;)

like image 34
c0cr0ach Avatar answered Nov 06 '22 10:11

c0cr0ach


@Benjamin Cremer thanks for the code, works like charm :) after reading this I did it using 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);
            }
        });
    });
});
like image 1
pahan Avatar answered Nov 06 '22 09:11

pahan