Friday 5 August 2011

simple ajax call

This is part for the view script:

function addAdditional(lnk) {
        jQuery.ajax({
                        type: "POST",
                        url: "<?php echo $this->url(array('controller' => 'content', 'action' => 'vote'), 'default', true); ?>",
                        data: jQuery('#star-form').serialize()+'&content_id=' + <?php echo $this->content_id ?>,
                        success: function(res)
                        {
                            r = jQuery.parseJSON(res);
                            if(r.status == 'success')
                            {
                                if(r.saved == 'success') {
                                    $("#vote-button").next().html('Uspesno ste glasali!');
                                } else {
                                    $("#vote-button").next().html('Niste glasali. Pokusajte ponovo!');
                                }

                            }
                            else
                            {
                                $("#vote-button").next().html('Niste glasali. Pokusajte ponovo!');
                            }
                        }
                    });
        return false;
    }

Here is the controller action

public function voteAction() {
        $result = array(
            "status" => "",
            "saved" => "",
        );

        if ($ok) {
            //do code
            $result = array(
                "status" => "success",
                "saved" => "success",
            );
        } else {
            $result = array(
                "status" => "success",
                "saved" => "error",
            );
        }

        die(json_encode($result));
    }

No comments:

Post a Comment