A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

Zend Framework How To: Handling checkboxes using Zend_Form and jQuery

I am sharing this because it took me a while to figure out how to handle checkboxes using jQuery.

First off, let us create a checkbox using Zend_Form.

class forms_CoolForm extends Zend_Form
{
    public function __construct($options = NULL)
    {
        parent::__construct($options);
        $this->setName('cool_form');
 
        $content = new Zend_Form_Element_Textarea('notes');
        $content->setLabel('Content')
                ->setAttrib('class','my_textarea')
                ->setAttrib('id','textarea_id')
                ->addValidator('NotEmpty')
                ->setRequired(true);
 
 
        $submit = new Zend_Form_Element_Button('button');
        $submit->setLabel('Go')
               ->setAttrib('class','my_submit')
               ->setAttrib('id','submit_id');
 
        $ispublic = new Zend_Form_Element_Checkbox('is_public');
        $ispublic->setLabel('Allow anyone to view this post?')
                 ->setAttrib('id','is_public');
 
        $this->addElements(array($content, $ispublic, $submit));
    }
}

The code for the Zend_Form_Element_Checkbox above will generate something like this:

<input type="checkbox" value="0" id="is_public" name="is_public"/>

Notice that the value is always Zero? Usually, in jQuery, we get used to getting the value of inputs using:

$('#is_public').val();

With that we can do all sort of things. But .val() will always return “0″. So the trick is, you need to check if the checkbox is “Checked” using something like this:

if($('#is_public').is(':checked')) {
    temp_public = 'yes';
} else {
    temp_public = 'no';
}
//then you send temp_public to other functions -- AJAX stuff or DOM modification

That’s about it. Basically it just the .is() in jQuery.

This entry was posted in General and tagged , , . Bookmark the permalink.

9 Responses to Zend Framework How To: Handling checkboxes using Zend_Form and jQuery

  1. Pingback: ZF: Handling checkboxes using Zend_Form and jQuery | PHP Readings

  2. Luca says:

    Thanks for sharing!It might usefull for me.

  3. Amit Shah says:

    How could we apply any JavaScript function on checkbox?

  4. Wenbert says:

    @Amit Shah, use jQuery and do something like this:

    if($(‘#is_public’).is(‘:checked’)) {
    //blah
    }

    where #is_public is the ID of the checkbox.

  5. Lalit S says:

    But where to write this jQuery code ..
    like write this code into a .js file and include it in our view file using tag ???

  6. Lalit S says:

    i meant the script tag ..

  7. Wenbert says:

    @Lalit, you can write it in the head tag or in the body. You can put it anywhere you want. You can even include it in a file.

    If you are using a .js file take a look at this post: http://blog.ekini.net/2009/05/15/zend-framework-adding-javascript-files-depending-on-controller/

    It will allow you to include files depending on which controller was loaded.

  8. N i B o H s says:

    How can I upload images in zend frame works?
    Hai I am creating a website using php, pgsql
    I am using zend frameworks for development.
    I have to save images in bytea format to pgsql
    any body can help me plse?

  9. nico says:

    many thanks !

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>